简体   繁体   English

php - laravel:如何处理数据库中的时间重叠?

[英]php - laravel : How to handle time overlapping in database?

I want to save two time interval in my database.我想在我的数据库中保存两个时间间隔。 where it will check the database and if someone already booked that time it won't save the time whereas if it's empty it will save time which user will give as a input.它将检查数据库,如果有人已经预订了那个时间,它不会节省时间,而如果它是空的,它将节省用户将作为输入提供的时间。

Eg.例如。 A user want to book the schedule 8:00 to 8:30, while saving into the database it will check in the database whether someone already take that time or not, if it's not then it will save otherwise it won't.用户想要预订 8:00 到 8:30 的时间表,同时保存到数据库中,它会在数据库中检查是否有人已经占用了该时间,如果没有,则将保存,否则不会。 Meanwhile user can't give input even in 8:15 also.同时用户即使在 8:15 也无法输入。

How do i solve this overlapping problem?我如何解决这个重叠问题?

here is the controller code I have used, it doesn't running though:这是我使用的控制器代码,但它没有运行:

 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'));

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

            $messages ="Class Room Already Taken";
            if ($classRoomCount > 0) {
             return redirect('allocateRoomPage',$message);
                    }
            else {
                 $classRoom->save();            
            return redirect('allocateRoomPage'); 
            }

    }

The rule for time overlapping is simple (see here for a complete graphic explanation):时间重叠的规则很简单(请参阅此处以获取完整的图形说明):

start1 < end2   AND   end1 > start2

So your query can be:所以你的查询可以是:

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

Firstly, to be able to have access to $startTime and $endTime within the query closure you will need to pass them through using the use construct ie首先,为了能够在查询闭包中访问$startTime$endTime ,您需要使用use构造传递它们,即

function ($query) use ($startTime, $endTime)

The following should work to get the correct counts for classrooms booked between certain times:以下应该有助于获得在特定时间之间预订的教室的正确计数:

$classRoomCount = ClassRoom::where(function ($query) use ($startTime, $endTime) {

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

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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