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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?