Packy Packy - 1 month ago 9
PHP Question

php array combine values - laravel

I have an data model for sales that looks like:

| id | dist_abbv | invoice | account | brand | quantity


Now when I dump out an account sales I will get an array (actually a laravel collection) like:

[0]=>Sale

"id" => 2113
"dist_abbv" => "CARDMT"
"date" => "2016-06-23"
"invoice" => 597935
"brand" => "ID46C"
"quantity" => 1
"account_vip_id" => 10010


How can I loop through this and combine all the same
brands
and total up the quantity? So opposed to 10 entries with only 2 different brands I get 2 entries with each brands total sales?

Answer

Try to use :

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

Hope this helps.

Comments