Basically i have a project in which i have to create a reservation for anything say computer's reservation on cyber shop so suppose one user has start time of reservation is 6:00 PM to 7:00 PM.I have two columns to store the start and end time named start_time and end_time. Suppose second user came and creating a reservation for 6:30 PM to 7:30 Pm then this user get prompts that computer is booked in between this time,3rd scenario will be reservation from 5:30 PM to 6:30 Pm ,4th scenario will be 06:30 to 06:45 PM ,4th scenario will be 5:00 pPM to 8:00 PM. Simply we can say that if the start_time or end_time lies between any reservation then i have to prompt a error. I am unable to do the same validation.
2 Answers
I think I have a solution, in which you have to receive the request, and using eloquent, but first you must take into account that, in order for it to work and the data to be returned, you must send the time data in 00:00:00 format.
public function getReservations(Request $request){
$startTime = $request->start_time; //something like '18:30:00'
$endTime = $request->end_time; //something like '19:30:00'
$date = $request->date; //your date consulting '2020-07-02'
$reservations = YourModelHere::select('date', 'start_time', 'end_time')
->whereDate('date', $date)
->where(function ($query) use ($startTime, $endTime){
$query->where(function ($query) use ($startTime, $endTime){
$query->whereTime('start_time', '>=', $startTime)->whereTime('start_time', '<', $endTime);
//You search if any data in your column is between in the $startTime and $endTime
})
->orWhere(function ($query) use ($startTime, $endTime){
$query->whereTime('end_time', '>', $startTime)->whereTime('end_time', '<=', $endTime);
//You search if any data in your column is between in the $startTime and $endTime
});
})
->first(); //if you only want get one reservation
//->get(); if you want get n reservations
}
This way you make sure that any time you define you will look for if any data in any of the two columns is between the time you receive. Take a look in laravel documentation It is the first time that I have answered a question, so I hope I have explained myself and that it will help you.
whereBetween('start_time', [date('h:i A', strtotime($request->start_time)), date('h:i A', strtotime($request->end_time))])
->orwhereBetween('end_time', [date('h:i A', strtotime($request->start_time)), date('h:i A', strtotime($request->end_time))])
->orWhere('start_time', '<=', date('h:i A', strtotime($request->start_time)))
->Where('end_time', '>=', date('h:i A', strtotime($request->end_time)))->count();
reservation_start
andreservation_end
user wants to reserve on timesstart_time
andend time
----- ifreservation_start is between start_time and end_time
orreservation_end is between start_time and end_time
then it not possible to reserve?