bartoszlsk
bartoszlsk

Reputation: 9

Laravel select count in raw query

I need to get count using Eloquent in Laravel. My query:

$sqlQueryCount = " SELECT COUNT(*)
        FROM client_order co 
        INNER JOIN client c ON c.id = co.client_id
        LEFT JOIN user u ON u.id = co.user_id
        WHERE co.user_id = ?
         ";
        
       $resultCount = DB::select($sqlQueryCount,
            [$userId])->count();

It doesn't work, cannot use count() on array.

Upvotes: 0

Views: 650

Answers (1)

omar esmaeel
omar esmaeel

Reputation: 572

Try something like this

DB::table('client_order')
        ->join('client ', 'client.id', '=', 'client_order.client_id')
        ->leftJoin('user ', 'user.id', '=', 'client_order.user_id')
        ->where('client_order.user_id',$user->id)->count();

Upvotes: 2

Related Questions