Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yadhul's avatar

convert this query into laravel query builder

SELECT booking_id, agent_id from bookings where booking_id in (

select receipts.booking_id from receipts join bookings where receipts.date <= DATE_ADD(bookings.date, INTERVAL 15 DAY) GROUP by receipts.booking_id having sum(receipts.net_amt) >= '50000'

) and

date BETWEEN '2018-01-01' and '2018-03-05'

0 likes
4 replies
yadhul's avatar

DB::table('bookings') ->select('booking_id','agent_id') ->whereIn('booking_id',function($query) use($target_days,$target_amount){ $query->selectRaw('receipts.booking_id from receipts join bookings where receipts.date <= DATE_ADD(bookings.date, INTERVAL '.$target_days.' DAY) GROUP by receipts.booking_id having sum(receipts.net_amt) >= '.$target_amount.''); }) ->whereBetween('bookings.date', [$start_date,$end_date]) ->get();

i made it.. worked for me

1 like
meeshka's avatar

@yadhul To make this work with Eloquent methods, you will have to use joins. Sometimes, if performance is an issue, it's fine to use queries like above.

Please or to participate in this conversation.