1

I have building a laravel application where in a controller I'm checking with time overlapping problem.

The Logic I have used is 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 so it can't let user to save the input otherwise if the query failed, it will let user to save the data.

public function postAllocateRoom(Request $request)
    {


            $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 = ClassRoom::where('day_id', $dayId)
                                                ->andWhere('start', $startTime)
                                                ->andWhere('end', $endTime)
                                                ->exists();

            if($timeExists){
                return 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'); 


}  

But After I run the program I'm seeing the following Error:

BadMethodCallException in Builder.php line 2258: Call to undefined method Illuminate\Database\Query\Builder::andWhere()

If anyone find the problem please help me to find the solution.

2 Answers 2

1

Simply use

 $timeExists = ClassRoom::where('day_id', $dayId)
                                            ->Where('start', $startTime)
                                            ->Where('end', $endTime)
                                            ->exists();

as there is no andWhere method in laravel

Sign up to request clarification or add additional context in comments.

Comments

1

There is no andWhere method.
Simple where(<...>)->where(<...>) acts like where <...> and where <...>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.