sometimes it might be easier to turn the problem on its head.
Could you have all the appointments in an array and then apply the time to them instead of the other way around?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm currently trying to make a job that takes a division, and makes a whole week of events in a schedule, however, I want to ensure that there isn't a dupe event for the same timeslot. A day has 4 timeslots, and if the first time slot is already occupied I want the job to take the second, and if that's occupied, and so on, if not any timeslots is available for the given day, it should check the next, if it isn't a saturday / sunday
My code:
public function handle()
{
// Purpose of job: add an activity to every academic in the division
// 1. check if there is an empty slot in the schedule for the days in that week
// 2. if there is, then add the event to the slot
foreach($this->division->academics as $academic)
{
$timeslots = [
now()->hours(8)->minutes(10)->seconds(0),
now()->hours(10)->minutes(0)->seconds(0),
now()->hours(12)->minutes(0)->seconds(0),
now()->hours(13)->minutes(45)->seconds(0)
];
do {
// do logic, if do-while would work?
} while ($academic->hasEmptySlots()); // ->hasEmptySlots returns bool wheter there are empty slots in the schedule
$end = $timeslots[0]->addHours(1.5);
// Subject to changes
Schedule::create([
'academic_id' => $academic->id,
'room_id' => Room::first()->id,
'subject_id' => Subject::first()->id,
'teacher' => 121,
'title' => 'Lorem Ipsum',
'description' => 'lol',
'start_time' => now()->hours(8)->minutes(10)->seconds(0),
'end_time' => now()->hours(8)->minutes(10)->seconds(0)->addHours(1.5)
]);
}
}
Please or to participate in this conversation.