Jonjie
1 month ago
232
2
Laravel

How to add where inside DB::raw() in laravel

Posted 1 month ago by Jonjie

I'm want to get the completed orders amount, and the rejected orders count in 1 query. Please see my code below. How can I add where or condition inside the DB::raw()?

return DB::table('restaurants')
->join('orders', 'restaurants.id', 'orders.restaurant_id')
->select([
   DB::raw('SUM(orders.total) WHERE status = 8 as completed_sum'),
   DB::raw('COUNT(DISTINCT(orders.transaction_code)) WHERE status = 3 as rejected_count')
])
->get();

Error

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

Please sign in or create an account to participate in this conversation.