$isBooked = Booking::where('apartment_id', request('apartment_id'))
->whereDate('starting_at', '>=', request('starting_date'))
->whereDate('ending_at', '<=', request('ending_date'))
->get();
Checking Between Date Range
I have a hotel room booking system, I want to check if a room is booked between 2 dates.
I have a form that takes starting date and ending date
In the bookings table i have 3 columns with names apartment_id, starting_at and ending_at, it is used to store the duration of the booking for an apartment.
I need a query that can take the form inputs and check it against the ones in the database.
Try this:
// assuming your form fields are named
// starting_date, ending_date and room_id
$isBooked = DB::table('bookings')
->where('room_id', request('room_id'))
->where('starting_at', '<=', request('ending_date'))
->where('ending_at', '>=', request('starting_date'))
->exists();
return $isBooked ? 'yes' : 'no';
Check if you need the equal sign on the comparisons in general you can have a booking starting in the same day another booking ends, because the first guest leaves at morning and the second one enters afternoon.
If you can't have any overlap even on boundaries change <= to < and >= to > on the conditions above
more info here: https://stackoverflow.com/a/325964
Please or to participate in this conversation.