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.