Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

Here is the scenario:

orders
 - id
 - qty

deliveries
 - id
 - qty
 - order_id

I want to use a join to display the orders with incomplete deliveries as well as the corresponging balance:

Order::left_join('deliveries', 'orders.id', '=', 'deliveries.order_id')
     ->select(array('orders.*'), DB::raw('orders.qty - IFNULL(sum(deliveries.qty),0) AS balance')))
     ->group_by('order_id')
     ->having('balance', '>', 0)
     ->get();

The 'balance' value works fine without the 'having' clause. On adding the 'having' clause however, the resulting table doesn't display any rows. Does anyone have any ideas?

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
359 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...