Eloquent Booking System
Hi,
I'm trying to build a booking system where people can search for rooms in hotels by inputting location , checkin/checkout dates.
I have these tables
properties (model Property) hotels (model Hotel) ( hotels is has a polymorphic relation shipt with properties ) rooms (model Room) (rooms are belongs to properties) bookings (model Booking) (bookings are belongsTo rooms)
Here is my approach so far.
- Firstly, I get rooms that have bookings within the time period the user is searching for.
$checkIn = $request->checkIn;
$checkOut = $request->checkOut;
$Bookings = Booking::where( function($query) use ($checkIn, $checkOut){
// checkIn BETWEEN check_in,check_out
$query->orWhere( function($query) use ($checkIn) {
$query->where('check_in', '<=', $checkIn);
$query->where('check_out', '>=', $checkIn);
});
// checkOut BETWEEN check_in,check_out
$query->orWhere( function($query) use ($checkOut){
$query->where('check_in', '<=', $checkOut);
$query->where('check_out', '>=', $checkOut);
});
// check_in,check_out BETWEEN checkIn,checkOut
$query->orWhere( function($query) use ($checkIn, $checkOut){
$query->where('check_in', '>=', $checkIn);
$query->where('check_out', '<=', $checkOut);
});
// checkIn,checkOut BETWEEN check_in,check_out
$query->orWhere( function($query) use ($checkIn, $checkOut){
$query->where('check_in', '<=', $checkIn);
$query->where('check_out', '>=', $checkOut);
});
})->get();
02 ) Then I filter out those rooms and select all the other available rooms
$not_available_room_ids = [];
foreach ( $Bookings as $key => $Booking){
array_push($not_available_room_ids, $Booking->room->id);
}
// GET AVAILABLE ROOMS
$Rooms = Room::whereNotIn('id', $not_available_room_ids);
Now I have all the availabe rooms which can be book.
Question :
Now in the view, what I do is loop through the result set of rooms. But these rooms have property (barbecue rooms are belongs to properties).
But what I want to do is , show each property as result item and show the available rooms of that property.
How can I do this ? Is this possible with my table and relationships structure ?
(here is how i currently have my result set [rooms]. These two rooms are from same property)
[
{
"id": 3,
"property_id": 2,
"name": "Luxury Suit",
"price_per_guest": 150
},
{
"id": 4,
"property_id": 2,
"name": "Budget Room",
"price_per_guest": 75,
}
]
(here is how i want them to be)
{
'id (property_id)': 2,
'name (property_name)' : 'Blah blah blah',
. . .
'rooms': [
{
'id: 3,
. . .
},
{
'id': 4,
. . .
}
]
}
any help would be appreciated
Thanks !!
Please or to participate in this conversation.