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

smartnathan's avatar

Hotel rooms booking system.

Please, am building a hotel management system. I have two models Rooms and Booking. Rooms will list all this rooms in the hotel. But I want to be able to list only rooms that hasn't been booked. Please, how do I solve this with Eloquent or query builder.

0 likes
8 replies
larafam's avatar

Room::where('is_booked', false)->get();

1 like
ondes's avatar

First of all you shall create some column in the Rooms table, which would represent booked state of that particular room- something like column "room_availability" where f.e. value 0 will represent "the room is free" and value 1 will represent "the room is not free". So obvioulsy, those rows in rooms table which will have 0 in "room_availability" column are free. You just change those 0s, 1 s representing availability room....if someone books that room, you place num 1 into that row at the availability column so for other bookings this wouldnt be available. You dont have to use numbers, thats just my .

Then if following my example, when 0 in the room_availability column means "room is free" you just simply do this one by eloquent:

$freerooms=Rooms::where('room_availability',0)->get();

Then you just foreach that....

Thats it.

Krisell's avatar

In addition to previous comments, it may also be a good idea to use a timestamp, booked_at rather than a boolean value. Make it nullable and the value null means it is not booked, and when booking you set 'booked_at' => Carbon::now(). That way, you can for instance find all rooms booked the last day, week, or whatever.

larafam's avatar

@Krisell room booking history should be in another table, i think room table should only state if the room is_booked or not

Krisell's avatar

@larafam Good point, however could we not fetch the availability from the same relation then, and not store any booking-status directly on the room (unless we want to avoid that for performance resons)?

smartnathan's avatar

@larafam, @ondes and @Krisell I don't know how to thank you all. I really do appreciate for all your concerns and assistance. May the Good Lord Bless you abundantly. Thanks once again.

smartnathan's avatar

Please, what if a guest wants to book a room a week ahead. Please, what is the best way to ensure that the rooms are still available on the list. Where the Room model manages all rooms and the Booking model manages all rooms being booked. I'm really concern about the query using eloquent or query builder.

Please or to participate in this conversation.