2

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?

1 Answer 1

3

Try to use :

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

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.