This episode is for members only

Sign up to access "Build an Appointment Booking System With Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
08. Iterating over requested days in a period

Episodes

0%
Your progress
  • Total: 4h 49m
  • Played: 0m
  • Remaining: 4h 49m
Join or sign in to track your progress

Transcript

00:00
OK, so hopefully we've got to grips with the concept of a date period and adding and subtracting. So for our four period method, let's get rid of the examples that we added in there. We'll also get rid of this die dump as well.
00:15
And let's go back to our web routes here and actually add in a period in here. So I'm going to do this very explicitly because when we work with calendars, of course, we're going to look from a certain day to, you know, the end of a certain period. So we could use the now helper actually and just say start of day.
00:33
Let's go ahead and do that. And then let's bump this up a little bit and go a little bit further into the future. So we're going to say add month and we're always going to say end of day because we always want to look at the end of the last date that we requested.
00:47
Otherwise, it's going to be the current date and time. If it was 12 o'clock today and we added a month, it would only look up to 12 o'clock in a month's time, which is not what we want. OK, so we've got this period in here.
00:59
Let's go ahead and accept these two periods into what we have here. So these are both going to be just carbon instances. So we're going to call this starts at and we're going to have another carbon instance for ends at.
01:13
Now, the way that this is going to work is we're going to build up eventually a collection of dates within a custom collection. So we're going to have, in this case, today, tomorrow, the next day, all the way up to a month's time.
01:26
But what we want to do when we're checking employee availability is we want to look on a day-by-day basis. We don't just want to grab that entire period and see when that employee is available. Now, the reason for that, if we look at our schedules table, we could do if we just had
01:43
a starts at and ends at date. But that assumes that the employee works every single day. We know that's not the case. We know that they might have certain days off in the week.
01:53
So what we want to do, first of all, is with the starts at and ends at date, break these up into days. Now, we don't need to use the period collection or anything from that package to do this. We can just use a standard date period.
02:07
So I'll just go ahead and show you what that looks like. We're going to go ahead and use a carbon period object, which, of course, comes from the carbon library. And we're going to go ahead and create out a carbon period.
02:18
And we're going to say starts at and ends at. Let's take a look at what this looks like in the browser. And you can see, sure enough, the start date here is the start of today. And the end date here is the end of the day in one month's time.
02:32
Great. Now, what we can do with the carbon period is we can grab the days from this. And we can iterate over this. So let's just go and use this days method.
02:41
And we'll die dump on this. So let's just have a look. We've got our start date and our end date. This is now iterable.
02:48
So let's go ahead and collect these up and iterate over them. We could just iterate over them normally. But I'm going to go ahead and use a Laravel collection here. So I'm going to say collect these up and then go over each of the days.
03:00
So let's create out a function in here, a closure in here. And we'll grab each of the days. In this case, though, I'm going to call it a date just so we can keep the terminology the same.
03:09
OK. So in here, we're just now going to dump, not die dump, on that date. And we'll see what we get. So give that a refresh.
03:16
And sure enough, we get every single day now within that period. And all of these days, of course, you can see that they match up exactly to what we want, the 10th, the 11th, the 12th, all the way up to the end date that we've requested. Now from these days, what we can do is extract out which day it is.
03:34
So I can find out if one of these dates is a Monday, a Tuesday, Wednesday, so on. And then we can use that to check the availability for an employee within that particular range. So for example, we could say something like format and L. And that will give us the day.
03:51
So you can see here, we've got Tuesday, Wednesday, Thursday, Friday, Saturday. It is currently Tuesday. So I'm recording on a Tuesday. Obviously, that's going to be the first day.
04:00
And then that goes all the way up to the last day that we have. So now we have a representation of each of the days. And that means that using that, we can check whether that specific employee in the database works on that day.
04:12
Then we can gather the hours that they work and use that to work out when they're available on that particular day. So hopefully, that makes sense. That's just the really basics of iterating over the days that we want and why we're doing
04:24
that. And again, that is why we included these Monday starts at Tuesday starts at columns. Because this makes it really, really easy and fast to check whether we're available on that particular day.
04:36
So that's pretty much it for now. Now that we've understood that, let's move over to the next episode where we're actually going to add the available periods for an employee based on this date that we've extracted out from when we're requesting our appointments for.

Episode summary

In this episode, we're building on our understanding of working with date periods using the Carbon library. We start by clearing out the previous example code and focus on what you need when working with a calendar: defining a clear start and end date for your period.

We explicitly set up a period starting from today (using the start of the day) and ending at the end of the day one month from now. It's explained why it's important to use the end of day, so the range covers the full days you intend, not just up to the current hour.

Next, we introduce how to break up this period into individual days. You'll see how to use Carbon's Period class to easily generate all the dates between your start and end points. We then collect all those days, turn them into a Laravel collection, and iterate over them.

From there, we show how to extract the specific day of the week for each date, which means you can later check database records for employee availability per day (since employees don't always work every single day).

Finally, with this list of days and their names, you'll have the groundwork to determine when an employee is available, day-by-day, within your requested period. This is a crucial building block for setting up flexible and accurate schedule and appointment logic. We'll use this foundation in the next episode to actually find available periods for each employee!

Episode discussion

No comments, yet. Be the first!