Implementing group_by and having in Laravel using Eloquent

后端 未结 1 1095
时光取名叫无心
时光取名叫无心 2020-12-11 18:18

I am having trouble implementing the group_by and having queries using Eloquent in Laravel.

Here is the scenario:

orders
 - id
 - qty

deliveries
 -         


        
相关标签:
1条回答
  • 2020-12-11 18:22

    Ended up switching to Laravel 4 and doing the ff which seemed to work.

    Order::leftJoin('deliveries', 'orders.id', '=', 'deliveries.order_id')
     ->select(array('orders.*'), DB::raw('orders.qty - IFNULL(sum(deliveries.qty),0) AS balance')))
     ->groupBy('order_id')
     ->havingRaw('balance > 0')
     ->get();
    
    0 讨论(0)
提交回复
热议问题