This episode is for members only

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

Get started
Already a member? Sign in to continue
Playing
06. Creating the appointments table

Episodes

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

Transcript

00:00
Lastly, let's work on our appointments schema and the relationships that we need.
00:05
So again, let's go ahead and create out a model for this. And that's, of course, going to be appointment. Let's open up the schema and get this working. So let's open up appointments table here and fill this in.
00:18
So the first thing that we want is something other than an ID. So we're going to choose a UUID and we're going to generate that automatically in this episode. And then we're going to have a foreign ID that relates to the service that the appointment is actually booked for.
00:34
So that's really important. We're going to do probably the same thing for our employee as well. And then similar to pretty much everything else, we're going to have a starts at and an ends at.
00:45
But this is going to be a date time because they're going to be combined. So let's say this starts at a certain time and ends at a certain time. Now, of course, what we could do with the starts at date is work out the ends at date by the service that's been booked.
00:58
But we're going to store it in this. So when we're going through and checking availability for a particular employee, we can use the starts at and ends at date to cut this out. It's going to be much easier to store these separately in the database.
01:12
OK, so last of all, let's create out just a time stamp in here whether this is cancelled or not. So I'm going to call this cancelled at. And that's also going to be nullable because obviously it's not always going to be cancelled. And then really, the rest of this is up to you.
01:27
So I'm going to choose just to store the name of the customer and the email address of the customer. But of course, you can customize this to whatever data you want to store in here. OK, let's go ahead and run our migrations again.
01:42
And we are done with our appointments table. Let's go straight over to the appointment model. And the first thing that I'm going to do is set guarded to an empty array. So we can just fill in whatever we need inside of here.
01:55
We also want to think about our casts as well. So we are going to cast the starts at date to a date time. Remember, this is a date object that has the time in as well now. And we'll do the same thing for ends at as well and cast that.
02:10
OK, so for the relationships here, again, we're going to have a service. So this belongs to a service. So let's just create that simple relationship out here. Belongs to a service.
02:20
And we'll also have an employee as well that this belongs to. So this belongs to an employee. And that is pretty much it for our relationships. And the last thing we want to do is automatically generate a UUID when this is created.
02:41
So let's go and create out a public static function called booted. So this will be called after this is booted. And then we're going to say while this is being created. So as it's creating, we are going to take the appointment.
02:56
And let's just type in that in there. And we're going to assign the UUID using Laravel String Helper. And we're going to use the UUID method. So that will generate a UUID as it's being created.
03:09
So when it is stored in the database, we will then have a UUID filled in there. We'll test this out much later when it comes to actually creating an appointment after we've gone through checkout. But for now, that is our appointments table done.

Episode summary

In this episode, we focus on setting up the appointments table in our application. We start by defining the appointments schema, making sure to include a UUID (which we set up to generate automatically for each appointment). We also add foreign keys to link each appointment to a specific service and employee, ensuring proper relationships within our data structure.

Next up, we add useful datetime fields: starts_at and ends_at, allowing us to keep track of the exact time slots for each appointment. We talk about why it's better to store both fields (rather than just one and calculating the other), especially for checking employee availability. There’s also a cancelled_at nullable field for tracking when (if ever) an appointment is cancelled.

For customer data, we keep it simple for now—a name and email—but we mention that you can customize this as needed. After the schema, we run database migrations to get the table up and working.

Then, we turn to the Laravel model for Appointment. We set up guarded fields, ensure our date fields are cast properly, and define the model relationships to both Service and Employee. To wrap up, we automate UUID generation whenever a new Appointment is created using Laravel’s String helper inside a booted function.

By the end of this episode, our appointments table is all set up and ready to be used. We’ll come back to test and use this when we handle creating appointments after checkout.

Episode discussion

No comments, yet. Be the first!