php Laravel Eloquent 关系列的总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21679678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent Sum of relation's column
提问by theAdmiral
I've been working on a shoppingcart application and now I've come to the following issue..
我一直在研究购物车应用程序,现在我遇到了以下问题..
There is a User, a Product and a Cart object.
- The Cart table only contains the following columns: "id", "user_id", "product_id" and timestamps.
- The UserModel "hasMany" Carts (because a user can store multiple products).
- The CartModel "belongsTo" a User and CartModel "hasMany" Products.
有一个用户、一个产品和一个购物车对象。
- 购物车表仅包含以下列:“id”、“user_id”、“product_id”和时间戳。
- UserModel "hasMany" Carts(因为用户可以存储多个产品)。
- CartModel“属于”一个用户,CartModel“有许多”产品。
Now to calculate the total products I can just call: Auth::user()->cart()->count().
现在计算我可以调用的总产品数:Auth::user()->cart()->count().
My question is: How can I get the SUM() of prices (a column of product) of the products in cart by this User?
I would like to accomplish this with Eloquent and not by using a query (mainly because I believe it is a lot cleaner).
我的问题是:如何获得此用户购物车中产品的价格(产品列)的 SUM()?
我想使用 Eloquent 而不是使用查询来完成此操作(主要是因为我认为它更简洁)。
回答by user1669496
Auth::user()->products->sum('price');
The documentation is a little light for some of the Collectionmethods but all the query builder aggregates are seemingly available besides avg()that can be found at http://laravel.com/docs/queries#aggregates.
该文档对某些Collection方法略显简单,但除了avg()可以在http://laravel.com/docs/queries#aggregates找到的之外,所有查询构建器聚合似乎都可用。
回答by ahmad ali
this is not your answer but is for those come here searching solution for another problem. I wanted to get sum of a column of related table conditionally. In my database Deals has many Activities I wanted to get the sum of the "amount_total" from Activities table where activities.deal_id = deal.id and activities.status = paid so i did this.
这不是您的答案,而是为那些来这里寻找另一个问题的解决方案的人准备的。我想有条件地获得一列相关表的总和。在我的数据库 Deals 中有许多活动,我想从活动表中获得“amount_total”的总和,其中活动表中的活动。
$query->withCount([
'activity AS paid_sum' => function ($query) {
$query->select(DB::raw("SUM(amount_total) as paidsum"))->where('status', 'paid');
}
]);
it returns
它返回
"paid_sum_count" => "320.00"
in Deals attribute.
在 Deals 属性中。
This it now the sum which i wanted to get not the count.
这现在是我想要得到的总和而不是计数。
回答by Abdur Rahman Turawa
I tried doing something similar, which took me a lot of time before I could figure out the collect() function. So you can have something this way:
我尝试做类似的事情,这花了我很多时间才弄清楚 collect() 函数。所以你可以有这样的东西:
collect($items)->sum('amount');
collect($items)->sum('amount');
This will give you the sum total of all the items.
这将为您提供所有项目的总和。
回答by Ahmed Mahmoud
Also using query builder
还使用查询构建器
DB::table("rates")->get()->sum("rate_value")
To get summation of all rate value inside table rates.
获得表中所有费率值的总和。
To get summation of user products.
得到用户产品的总和。
DB::table("users")->get()->sum("products")

