Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

experimentor's avatar

mistake in my code

if ($schedule->exists())

should be

if($existing_schedule)

if the combination of teacher, subject, room, start time, end time and day is not in data base, it will get saved as a new schedule. Otherwise it will dd('Exist')

Also put a return statement after $schedule->save();

tykus's avatar

OP hasn't yet consider that a teacher or the room might also be assigned to a different Schedule at the same time...

vincentsanity's avatar

why i get SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'subject_code_id' cannot be null..... but in my request payload i get

id: 3, term: "2", semester: "2", start_time: "10:30 PM", end_time: "12:30 AM", scid: 10,…}
id: 3
term: "2"
semester: "2"
start_time: "10:30 PM"
end_time: "12:30 AM"
scid: 10
teacher_id: 5
room_id: 9
subject_code_id: 3
day: "M-W-F"
school_year: "2020"

also no need to get the current schedule id? @capper33 ?

Snapey's avatar

and what when you need to update THIS schedule?

Have you given any thought to what you are trying to do?

What are the business rules ?

as @tykus says, what if you change the teacher on an existing schedule? Does that matter? Can you update the schedule with a different teacher? What if the teacher is on a completely different schedule at that time?

Same for the room?

You can create a duplicate if you just change the room? Is that ok?

These things you can test for in your logic They are not really validation concerns.

Mohammed-H's avatar

@vincentsanity Man forget the code try to write in few sentences what you want to achieve and post it, otherwise gonna be hard for us to help you :)

vincentsanity's avatar

What i want to achieve is if you update a certain schedule and if the requests is already existing in the database it will not update hence, it will dd('something') thats what i want to achive in updating the schedule. Because the schedules table must have a different values in every fields in it. Becasuse for exampe. The room_1 is occupied in start_time = 2:30 and day = Monday-Friday and i add or update a schedule and put the same value then it must be not accepted. Also it applies to teachers and overall. Hope you understand what i mean sir @snapey @mohammed-h .

right now what i did is

 $schedule = Schedule::findOrFail($id);

        $validate = Schedule::where('subject_code_id',$request->subject)
                    ->where('teacher_id',$request->teacher)
                    ->where('room_id',$request->room)
                    ->where('start_time',$request->start_time)
                    ->where('end_time',$request->end_time)
                    ->where('school_year',$request->schoolyr)
                    ->where('day',$request->days)
                    ->where('term',$request->term)
                    ->where('semester',$request->sem)
                    ->first();
        if($validate){
            dd('exist');
        }else{
            $schedule->update($request->all());
        }

Seems not working

Mohammed-H's avatar

Try to use orWhere instead of where

$validate = Schedule::where('subject_code_id',$request->subject)
                    ->where('teacher_id',$request->teacher)
                    ->orWhere('room_id',$request->room)
                    ->orWhere('start_time',$request->start_time)
                    ->orWhere('end_time',$request->end_time)
                    ->orWhere('school_year',$request->schoolyr)
                    ->orWhere('day',$request->days)
                    ->orWhere('term',$request->term)
                    ->orWhere('semester',$request->sem)
                    ->first();
        if($validate){
            dd('exist');
        }else{
            $schedule->update($request->all());
        }
vincentsanity's avatar

Need to use orWhere in all request or only in the room and others? @mohammed-h

I keep getting exist. why?

tykus's avatar

Your first query is checking only for that teacher in that room at that time on those days during that term of that school year (etc.). If your request data (for update) is changing any of those params, you will not find the existing Schedule.

You will need to separately check the availability of the teacher and the room.

vincentsanity's avatar

basically i just dont want any duplicate values in my schedule table thats what i only want. If the schedule is click and there is no value changed in the form then if submit is clicked it will not update because it is existng,

Mohammed-H's avatar

I understand now. Here how you can achieve it

$schedules = Schedule::whereBetween('start_time', [$request->start_time, $request->end_time])
                      ->WhereIn('teacher_id', $request->teacher)
                                            ->orWhereIn('room_id', $request->room)
                                            ->get()


                                            if($schedules){
                                                dd('exixts')
                                            }
vincentsanity's avatar

cant execute the code Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, @mohammed-h

Mohammed-H's avatar

My bad try this


$schedules = Schedule::whereBetween('start_time', [$request->start_time, $request->end_time])
                        ->WhereIn('teacher_id', [$request->teacher])
                                            ->orWhereIn('room_id', [$request->room])
                                            ->get()
Mohammed-H's avatar

:) To see if there is any schedule that matches, otherwise, you'll get the Query Builder object as result and that doesn't help :). You can also use count() instead of get()

ahmedmansour08's avatar

if($update->count() == 0) { dd('Not Exist'); } else { dd('Exist'); }

Previous

Please or to participate in this conversation.