Reputation: 2505
I am building a laravel application where In a controller I want to book a room with some time gap and if someone already booked that room with that time it will forbid the user to book that room in that time interval. How do I solve the problem?
I have written a Query in my controller for checking time range but it's not working properly.
Eg: if a user want to book a room from 12:00 to 12:30, but someone already has booked that room from 12:15 to 12:45, controller won't let book that room.
Here is my controller:
public function postAllocateRoom(Request $request)
{
$classRoom = new ClassRoom();
$classRoom->department_id=$request->Input(['department_id']);
$classRoom->room_id=$request->Input(['room_id']);
$classRoom->course_id=$request->Input(['course_id']);
$classRoom->day_id=$request->Input(['day_id']);
$classRoom->start=$request->Input(['start']);
$classRoom->end=$request->Input(['end']);
$startTime = Carbon::parse($request->input('start'));
$endTime = Carbon::parse($request->input('end'));
$room=DB::select('SELECT allocate_rooms.id
FROM allocate_rooms
WHERE "' . $startTime . '" BETWEEN allocate_rooms.start AND allocate_rooms.end AND
"' . $endTime . '" BETWEEN allocate_rooms.start AND allocate_rooms.end');
$messages ="Class Room Already Taken";
if ($room) {
return redirect('allocateRoomPage');
}
else {
$classRoom->save();
return redirect('allocateRoomPage');
}
}
Upvotes: 0
Views: 380
Reputation: 21
The following logic checks whether the booking time interval is clash with the current allocated room's time interval or not. It's just the logic, you may need to modify as proper.
if (($booking_start_time < $allocated_room_start_time && $booking_end_time < $allocated_room_start_time) || ($booking_start_time > $allocated_room_end_time && $booking_end_time > $allocated_room_end_time)) {
// No time clash
}else{
// Time clashed !!
}
Hope this help.
Upvotes: 1