我有一个销售数据模型,如下所示:
| id | dist_abbv | invoice | account | brand | quantity
现在,当我转储帐户销售时,我将得到一个数组(实际上是一个 Laravel 集合),例如:
[0]=>Sale
"id" => 2113
"dist_abbv" => "CARDMT"
"date" => "2016-06-23"
"invoice" => 597935
"brand" => "ID46C"
"quantity" => 1
"account_vip_id" => 10010
我如何循环遍历并合并所有相同的品牌
并合计数量?那么与只有 2 个不同品牌的 10 个条目相反,我得到每个品牌总销售额的 2 个条目?
最佳答案
尝试使用:
DB::table('sales')
->select('brand', 'sum(quantity) as total')
->groupBy('brand')
->get();
希望这有帮助。
关于PHP 数组组合值 - laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40294973/