简体   繁体   中英

PHP - Laravel : How to save without time overlapping in DB

I have a situation where user will save time interval in database means schedule where it won't effect others time.

Eg: If a classroom is already booked for 7:00Am to 8:00am user can't save time in between that time means user can't save 7:30 to 8:30 or in that time interval.

How do I check the time interval and save the time it in laravel?

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']);        
            $classRoom->save();            
            return redirect('allocateRoomPage'); 
    }

I have two columns in my db with the name of start and end type: Time

If you want check it in server side you should make additional request to DB for checking time.

$startTime = Carbon::parse($request->input('start'));
$endTime = Carbon::parse($request->input('end'));

$classRoomCount = ClassRoom::where(function ($query) {
    $query->where('start', '>=', $startTime)
          ->where('end', '<=', $startTime);
})->orWhere(function ($query) {
    $query->where('start', '>=', $endTime)
          ->where('end', '<=', $endTime);
})->count();

if ($classRoomCount > 0) {
    echo 'Classroom is already booked';
}

您可以检查保存时是否使用了间隔,此外,还可以事先提取现有间隔以在客户端显示它们。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM