I'm building a reservation system for a smart lockers project.
The user should be able to book via the website a locker for the time he needs and obviously, I need to make sure that there are no overbookings.
I'm trying to build a query with eloquent to get total count of the bookings for the dates requested from the user.
So far, in my controller I have done the following :
public function create(Request $request)
{
$locationId = $request->input('location_id');
$beginningDate = Carbon::parse($request->input('beginning_date'))->format('d-m-Y');
$beginningTime = Carbon::parse($request->input('beginning_time'))->format('H:m');
$duration = $request->input('duration');
$totalPrice=PriceList::where('duration_to_hours', $duration)->value('price');
$reservationStartingDate = $beginningDate. ','.$beginningTime;
$calculateReservationDates = Carbon::parse($reservationStartingDate)->addHours($duration);
$endDate= $calculateReservationDates->format('d-m-Y');
$endTime = $calculateReservationDates->format('H:m');
$reservedCount = Reservation::where('beginning_date', '<=', $endDate)
->where('end_date', '>=', $beginningDate)
->where('beginning_time', '<=', $endTime)
->where('end_time', '>=', $beginningTime)
->count();
$totalBoxes = Box::where('location_id', $locationId)->count();
dd($totalBoxes, $reservedCount);
$available = false;
if($totalBoxes > $reservedCount){
$available = true;
}
return view('checkout', compact('locationId', 'totalPrice', 'endDate', 'endTime', 'beginningDate', 'beginningTime', 'duration', 'available'));
}
This query is working if the dates and time requested from the user are the same as the other reservation.
Let's suppose there's a reservation for 06/04/2022 from 7 pm for 2 hours and there's only one box available in total. If the new user try to make a reservation for the same time and dates, than it returns no availability but, if the user try to make a reservation for 06/04/2022 from 7.30 pm for 2 hours it returns that there's availability, when in reality it should return there isn't.
I don't get where I'm missing something, do you have any idea on how i can solve this matter?
Thank you