douzhannao5357 2016-05-12 17:09
浏览 102
已采纳

Laravel:如何避免时间重叠?

I have building a laravel controller where I'm trying to avoid time overlapping. But I'm facing problem with my query as I couldn't run the query properly in 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=Carbon::parse(str_replace(array('am', 'pm'), ':00', $request->input('start')));
            $classRoom->end=Carbon::parse(str_replace(array('am', 'pm'), ':00', $request->input('end')));
            $day = $classRoom->day_id;
            $startTime=$classRoom->start;
            $endTime=$classRoom->end;

        $result=DB::select( DB::raw("SELECT * FROM `allocate_rooms`
         WHERE  start='$startTime' AND end='$endTime' AND  day_id='day'"));
                  if (sizeof($result)>0) {
                 flash()->error('Class Room Already Taken.');
             return redirect('allocateRoomPage');
                    }
            else { 
                 $classRoom->save();  
                   flash()->success('Successfully allocated room.');          
                 return redirect('allocateRoomPage');  
            }                  
    }

Here in my controller's query first I will check whether the day_id has given as input is match with database with that day_id and then it will check with the time, if it matches the result will be more than one, so it can't let user to save the input otherwise if the query failed, it will let user to save the data.

I'm facing problem with the query. If any one help to find out the solution.

  • 写回答

1条回答 默认 最新

  • dsiy62758 2016-05-12 18:53
    关注

    First of all, I suggest you to perform some validations on your inputs before creating the model instance. Then, I don't understand why you sometimes use $request->Input(['input_name']) and sometimes $request->input('input_name'), it's better to use the second syntax.

    I edited your code, please test it, it should work.

    public function postAllocateRoom(Request $request)
        {
                // SOME VALIDATION HERE BEFORE GO ON, PLEASE
    
                $startTime = Carbon::parse(str_replace(array('am', 'pm'), ':00', $request->input('start')));
                $endTime = Carbon::parse(str_replace(array('am', 'pm'), ':00', $request->input('end')));
                $dayId = $request->input('day_id');
    
                $timeExists = AllocateRooms::where('day_id', $dayId)
                                                    ->where('start', $startTime)
                                                    ->where('end', $endTime)
                                                    ->exists(); //use allocate_rooms table model (I don't know if it is ClassRomm)
    
                if($timeExists){
                    reuturn redirect('allocateRoomPage')->withErrors(['time' => 'Class Room Already Taken']);
                }
    
                $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=$dayId;
                $classRoom->start=$startTime;
                $classRoom->end=$endTime;
                $classRoom->save();
    
                $request->session()->flash('success', 'Successfully allocated room');
    
                return redirect('allocateRoomPage'); 
    
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog