public static function getBookedDates($roomSlug = null) { // Retrieve room id from slug if (isset($roomSlug)) { $room = Room::select('id')->where('slug', '=', $roomSlug)->first(); $roomId = $room->id; } // Select booked dates // don't know why this doesn't work when separate requests oO if (isset($roomSlug)) { $booking = self::select('arrival', 'departure')->where('room_id', '=', $roomId)->whereValidated(1)->get(); } else { // No room id ? Get all booked dates of all rooms (in this case we consider that only one room exists) $booking = self::select('arrival', 'departure')->whereValidated(1)->get(); } // Construct array of booked dates $bookedDates = []; foreach ($booking as $d) { $arrival = explode('-', $d->{"arrival"}); $departure = explode('-', $d->{"departure"}); if (isset($arrival[0]) && !empty($arrival[0]) && isset($departure[0]) && !empty($departure[0])) { // Compute difference between arrival and departure, how many nights are booked ? $aDate = Carbon::create($arrival[0], $arrival[1], $arrival[2], 0); $dDate = Carbon::create($departure[0], $departure[1], $departure[2], 0); $diffInDays = $dDate->diffInDays($aDate); // for each booked day, add entry in the array for ($i = 1; $i <= $diffInDays; $i++) { $nextDate = $aDate->addDay()->format("Y-m-d"); $nextDate = explode('-', $nextDate); // Place year into array if (!in_array((int) $nextDate[2], $bookedDates)) { $bookedDates[''] = [(int) $nextDate[2] => []]; } // Place month into array if (!isset($bookedDates[(int) $nextDate[0]][(int) $nextDate[1]])) { $bookedDates[(int) $nextDate[0]][(int) $nextDate[1]] = []; } // Place day into array if (!isset($bookedDates[(int) $nextDate[0]][(int) $nextDate[1]][(int) $nextDate[2]])) { $bookedDates[(int) $nextDate[0]][(int) $nextDate[1]][] = (int) $nextDate[2]; } } } } return $bookedDates; }
public static function getBookedDates($room_slug = null) { // Retrieve room id from slug if (isset($room_slug)) { $room = Room::select('id')->whereSlug($room_slug)->first(); $room_id = $room->id; } // Select booked dates $booking = self::select('arrival', 'departure'); if (isset($room_slug)) { $booking = $booking->whereRoomId($room_id); } $booking = $booking->whereValidated(1)->get(); // Construct array of booked dates $bookedDates = []; foreach ($booking as $d) { $arrival = explode('-', $d->{"arrival"}); $departure = explode('-', $d->{"departure"}); if (isset($arrival[0]) && !empty($arrival[0]) && isset($departure[0]) && !empty($departure[0])) { // Compute difference between arrival and departure, how many nights are booked ? $aDate = Carbon::create($arrival[0], $arrival[1], $arrival[2], 0); $dDate = Carbon::create($departure[0], $departure[1], $departure[2], 0); $diffInDays = $dDate->diffInDays($aDate); // for each booked day, add entry in the array for ($i = 1; $i <= $diffInDays; $i++) { $nextDate = $aDate->addDay()->format("Y-m-d"); $nextDate = explode('-', $nextDate); // Place year into array if (!in_array((int) $nextDate[2], $bookedDates)) { $bookedDates[''] = [(int) $nextDate[2] => []]; } // Place month into array if (!isset($bookedDates[(int) $nextDate[0]][(int) $nextDate[1]])) { $bookedDates[(int) $nextDate[0]][(int) $nextDate[1]] = []; } // Place day into array if (!isset($bookedDates[(int) $nextDate[0]][(int) $nextDate[1]][(int) $nextDate[2]])) { $bookedDates[(int) $nextDate[0]][(int) $nextDate[1]][] = (int) $nextDate[2]; } } } } return $bookedDates; }