1

hope you all doing great.

I just moved to Laravel recently from CI.

I have two tables - Bookings - Booked_Rooms

What i want is if all the rooms are checked_out which i will decide by using Booked_Rooms.checkout_at is null then parent record should exclude from data set.

I tried google and did little bit research but couldn't find what i am actually looking.

I also tried Booking:with(array('rooms',function($q){ // where query })) but it is still fetching the parent record. I don't want to traverse every single record by loop and then excluding the records because it doesn't looks good for performance measurement. I also know that i can do this by using join but can i do this using Eloquent ORM

Summary : If all the checkout_at columns of all booked_rooms are not null then the parent booking record is considered as completed and shouldn't be in pending bookings.

HtlBooking Model Class

public function rooms()
{
    return $this->hasMany('App\BookedRoom','booking_id');
}

Controller Function

$bookings = HtlBooking::with ( 'rooms' )->with ( 'user' )
        ->get ();

1 Answer 1

3

Found my answer, just did it by using

$bookings = HtlBooking::with('rooms')->whereHas('rooms',function($q){$q->where('checkout_at',null);})->with ( 'user' )
        ->get ();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.