jeudi 1 octobre 2015

Laravel Eloquent with subquery for finding free time slots

I'm trying to make laravel subquery with eloquent for filter free time slots. I have query which returns free slots, rewriting is not the option in this stage.

I need eloquent to eject results that don't have reservation, because I get list of models and they have 'reservation' array empty, I need to remove those results from final output.

if (Input::has('checkin') || Input::has('checkout')) {
        $joins['reservation'] = function ($query) {
            $checkIn  = Input::get('checkin');
            $checkOut = Input::get('checkout');
            if ($checkIn != '' && $checkOut != '') {
                $checkin  = date('Y-m-d', strtotime($checkIn));
                $checkout = date('Y-m-d', strtotime($checkOut));
                $query->where('reservation_calendars.start_date', '<=', $checkin);
                $query->where('reservation_calendars.end_date', '=>', $checkout);
            }
        };
    }

    $villas = Villa::with($joins)->where('active', '=', 1);

How do I remove villas that dont have reservations (where reservations relation is empty)?

I dont want models with this in result

 "reservation" => Collection {#412 ▼
  #items: []
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire