Aug 31, 2020
2
Level 12
How to add where inside DB::raw() in laravel
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 or to participate in this conversation.