I am working in an online bike booking system. Where the customer can book a bike for particular date-time range. If another customer books the same bike for a particular date-time range, I have to check that bike is already booked or not. My database table booking is like this:
id bike_id start_date end_date
1 1 2019-05-04 14:30 2019-05-04 15:30
1 1 2019-05-04 16:30 2019-05-04 18:30
Suppose if another customer wants to book a bike for 2019-05-04 17:00 to 2019-05-04 19:30 (i.e. start_date to end_date) then the bike should not be available. Booking should available only if no other previous bookings overlap in requested date-time range. How to write a MySQL query for this in laravel?
you can use whereRaw to check a date between two columns
whereRaw('? between start_date and end_date', [date('Y-m-d')])
or
where(function ($q) {
$q->where('start_date', '<=', date('Y-m-d'));
$q->where('end_date', '>=', date('Y-m-d'));
})