2016-10-27 29 views
2

我有銷售,看起來像一個數據模型:PHP陣列相結合的價值觀 - laravel

| 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 

如何循環瀏覽併合並所有相同的brands並總計數量?因此,與僅有2個不同品牌的10個參賽作品相反,每個品牌的總銷售額中有2個參賽作品?

回答

3

嘗試使用:

DB::table('sales') 
    ->select('brand', 'sum(quantity) as total') 
    ->groupBy('brand') 
    ->get(); 

希望這有助於。