User57
User57

Reputation: 2505

Laravel : how to avoid time overlapping?

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.

Upvotes: 0

Views: 883

Answers (1)

Claudio King
Claudio King

Reputation: 1616

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


    }

Upvotes: 1

Related Questions