Welp?
Nested array
Hello there, I have a datatables on front end, grabbing content with AJAX, the problem is that I want to generate the table head and content (booking rooms). The desired result is the following:
Price Room number 01/08 02/08 03/08 04/08 And so on.
| £5 | 1 | B | B | F | R | R |
| --- | --- | --- | --- | --- | --- | --- |
| £5 | 2 | R | R | R | F | F |
I have the following table (rooms, reservations):rooms: id room_id name price_per_day max_persons And so on. 1 1 Garden view 5 2
|id | room_no | check_in | check_out | status|
|---|---|------------|----------|------------|----------|---|
| 1 | 1 | 01/08/2020 | 10:30:00 | 02/08/2020 | 10:30:00 | 1 |
| 2 | 1 | 04/08/2020 | 15:00:00 | 15/08/2020 | 15:00:00 | 2 |
| 3 | 1 | 25/07/2020 | 11:00:00 | 28/07/2020 | 11:00:00 | 3 |
Status legend: 1 = reserved; 2 = room is rented; 3 = concluded; price_per_day is joined with hasOne relatioship.
My idea was to create an array with dates:
$startDate = Carbon::parse($now)->startOfMonth();
$endDate = Carbon::parse($now)->endOfMonth()
$periods = CarbonPeriod::create($startDate, $endDate);
And after selecting everything from reservation table $allReservations = Rooms::with('reservations')->get(); Once I got everything I start to foreach the rooms and check if check-in date is greater or equal & check-out is less than equal than the period using something like this:
foreach($allReservations as $singleReservation){
foreach($periods as $currentDate){
//check if there's no reservation on a room
if($singleReservation->reservations->isEmpty(){
//do nothing
} else {
foreach($singleReservation->reservation as $currentReservation){
if($currentDate->gte($currentReservation->check_in) && $currentDate->lte($currenReservation->checkOut){
Add to array
}
}
}
}
I don't think that is the 'best' method to do this kind of stuff, since I want the array to be divided in two.
- Columns (Price | Room No. | 01/08 | 02/08 | 03/08| 04/08 | ... | 31/08)
- Data (£5 | 1 | B | B | F | R| ...| R) B stands for booked; F for free/available; R for rented. Maybe it's a simple task but I got lost here.
Please or to participate in this conversation.