In this episode, we focus on figuring out which days an employee is available to work within a given period—kind of a crucial next step for any scheduling app!
We start by passing an employee and a service into our constructor so we have all the info we need. From there, we look up the employee's long-term schedule and determine whether they can work on each day in our target period (e.g., the next month).
To do this efficiently, we pull all their schedules at once as a Laravel collection. This way, we avoid hammering the database with queries for every single day! We then loop through the days, checking if each one is covered by a schedule, and use a helper method to grab the right start and end times for each day based on the employee's schedule.
We also make sure to handle cases where the employee doesn't work on a particular day by returning null
. We even clean up the returned data so it only includes valid start/end combos—nothing half-finished.
On top of that, we take the service duration into account. For example, if the employee finishes at 5 pm, but a service takes 30 minutes, the last bookable slot should end at 4:30 (not at 5:00). We adjust each day's available period accordingly.
By the end, we've built a nice collection of all the time periods within a month that the employee can actually work, based on both their availability and the service duration. Slots are coming up next—so stay tuned!