User57
User57

Reputation: 2505

Laravel : Call to a member function where() on float

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

Answers (2)

Vahe Galstyan
Vahe Galstyan

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

LF-DevJourney
LF-DevJourney

Reputation: 28529

Laravel doc

The query builder also provides a variety of aggregate methods such as count, max, min, avg, and sum. 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

Related Questions