Reputation: 2505
I was trying to find sum of different column using Query Builder. Here is Query :
DB::table('carts')
->sum(\DB::raw('carts.price+COALESCE(carts.support,0)+COALESCE(carts.installation,0)' ))
->where('status','=',1)
->where('user_id','=',$id);
But when i dd()
the output i got the above Error.ie,
Call to a member function where() on float
How do i avoid that?
Upvotes: 1
Views: 12783
Reputation: 1731
You should call sum
at the end of the query:
DB::table('carts')
->where('status', 1)
->where('user_id', $id)
->sum(\DB::raw(
'carts.price
+ COALESCE(carts.support, 0)
+ COALESCE(carts.installation, 0)'
));
Upvotes: 3
Reputation: 28529
The query builder also provides a variety of aggregate methods such as
count
,max
,min
,avg
, andsum
. You may call any of these methods after constructing your query:$users = DB::table('users')->count(); $price = DB::table('orders')->max('price');
Of course, you may combine these methods with other clauses:
$price = DB::table('orders') ->where('finalized', 1) ->avg('price');
Upvotes: 1