$query = DB::select('offers.id', DB::expr("(SELECT COUNT( coupons.id ) FROM coupons WHERE
coupons.status = 'Y' AND coupons.offer_id = offers.id) AS `coupons_sold`"))
->from('offers')
->where('offers.status', 'IN', array('P', 'S'))
->where('offers.published_at', '<', $tomorrowMidnight)
->where('offers.end_at', '>', time())
->where('coupons_sold', '<', DB::expr('offers.max_available'))
->order_by('offers.created_at', 'DESC');
It looks like you're new here. If you want to get involved, click one of these buttons!