Packy
Packy

Reputation: 3593

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?

Upvotes: 2

Views: 70

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67525

Try to use :

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

Hope this helps.

Upvotes: 3

Related Questions