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

Givar's avatar
Level 1

Build availability query for reservation system

Hello everybody,

I'm building a booking system for a smart locker, where the user can reserve through the website a locker for a date and X hours.

I have a model Reservation and a model Box, I'm trying to query all the reservation that are already in place for the dates and time requested from the new user.

Example: You want to book a locker and you search free locker on 07/04/2022 from 7AM for 3 hours, at this point the system should check if there are available boxes for that date and time and if so, allow you to complete the reservation; if not, should say, sorry we are fully booked.

The reservations table has 4 main columns that interests me in this case :

-beginning_date
-beginning_time
-end_date
-end_time

What i have done so far, is the following :

 $reservedCount = Reservation::where('beginning_date',  '<=', $endDate)
        ->orWhere('end_date', '>=', $beginningDate)
        ->orWhere('beginning_time', '<=', $endTime)
        ->orWhere('end_time', '>=', $beginningTime)
        ->count();

The variables

$beginningDate
$beginningTime
$endDate
$endTime

are the dates and time requested from the user that wants to make a new reservation.

The above code doesn't seem to work or return the correct count and i really do not know how to get out of it. Do you have any idea on how i can get the correct count?

Hope that somebody can help me with this.

Thank you

-------------------------++++++++++UPDATE+++++++++++------------------------------------

I think that i have in part solved the above issue doing link this :

$reservedCount = Reservation::where('beginning_date', '>=', $endDate)
       ->where('end_date', '>=', $beginningDate )
       ->where('beginning_time', '<=', $endTime)
       ->where('end_time', '>=', $beginningTime)
       ->count();

But now, when testing, it returns the correct availability but not if the new bookings starts in the same hour of the booking that is ending.

To give you an example :

If i have a booking from 10 AM for 3 hours this booking will end at 1PM; but, if i try to make a new booking from 1PM for 3 hours, it returns that there's no availability, while there's.

At first seems like that it is not comparing minutes. Do you have any idea on how i can solve this?

Thank you everybody.

0 likes
8 replies
jlrdw's avatar

Have a look at laravel calendar.

Givar's avatar
Level 1

@jlrdw I saw it already. I don't think it is suitable with this case. I just need to check availability.

MohamedTammam's avatar

Why you aren't using datetime columns instead of storing it in date and time columns?

Snapey's avatar

Use datetime fields!

Can a booking span midnight?

Givar's avatar
Level 1

@Snapey do you mean columns or input fields? If input fields, i'm already using it.

Also i noticed that Carbon addHours() is not correctly calculating the time. If i book 10.40 am for 3 hours, it will return 1pm.

Yes it can, it can also be later than midnight becuse this is a 24hrs shop.

Please or to participate in this conversation.