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
16. Using a custom date collection

Episodes

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

Transcript

00:00
Let's go ahead and swap out this plain collection
00:02
that we've created here to push all of our days due to a custom collection. There's a really good reason for that. This is going to be the source of truth for all of the slots
00:10
that we create. At the moment, it's a little bit wonky because we've got too many slots. We're not pushing any employees there.
00:16
But I'm going to create this now because then later what we can do is add some helper methods to this date collection. And we're going to be doing two main things with this date
00:24
collection. The first one is grabbing the first available date. So we can go through all of the dates that we've got in here, check if they're empty, and grab out the first available one.
00:33
And we can also use this to check if we have any slots for that particular day. So we'll create this now. And then later on, it's just going
00:40
to be a breeze to just add more methods into this collection. OK, so to do this, we just go ahead and create out a standard class in here. And we're going to call this DateCollection.
00:50
Now, we can choose what this extends. You can do this manually if you're not using something like phpStorm. But we're basically just going to choose
00:56
that we want to extend the IlluminateSupportCollection class. And we're done. So if not, just go ahead and extend that manually.
01:04
OK, so now we've done that. We're going to leave everything alone in here. I'm not going to touch anything just in here yet. But we are going to switch this over to a new DateCollection.
01:14
And there we go. Perfect. So this is going to work in exactly the same way. Give it a refresh.
01:19
The only difference here is that we now have a DateCollection. We've got everything inside of it. It functions just like a normal collection. But we can add any help methods in here that we need.

Episode summary

In this episode, we start by refactoring our plain date collection and swap it out for a custom DateCollection class. There's a good reason for this! By having our own class, we can make it the single source of truth for all the slots we create in our app, which will make things much easier to manage going forward.

Right now, our slots system is a bit messy. We have too many slots, and we're not pushing any employees into them yet, but setting up the custom collection now is going to make it much easier to enhance our logic later. For example, we'll soon add helper methods—like one to grab the first available (empty) date, and another to check if a slot exists for a particular day.

We create a new DateCollection class and make it extend Laravel's base Collection class so it works just like a regular collection. For now, we're not adding any new methods yet—just switching over our code to use this new class. This means everything works exactly the same, but we're set up to keep our date logic organized as we move on. Give your app a refresh and you probably won't see any difference, but we'll thank ourselves later for making things easier to extend!

Episode discussion

No comments, yet. Be the first!