Need cart garbage collection0:00
We now have the ability to create database cards on the fly which is pretty awesome. It would be also nice to have some sort of garbage collection happening where the stale cards that sort of need to go away are cleared up from the database. What I've described in the intro just then might seem a bit intimidating and daunting. But as you'll see, Laravel makes it super easy. So I have three cards in the database at the moment, they've all been created manually
So I have three cards in the database at the moment, they've all been created manually by me or, well, these two were manual and this one was created using the Ensure Exists function on our card model. As you can imagine, as more people visit our sites, this is going to fill up real quick. We're going to set an expiry date to these cards and whenever the expiry has reached, we're going to automatically delete these cards from the database.
Adding model pruning rules0:46
reached, we're going to automatically delete these cards from the database. We're not going to do any of these manually, instead we'll rely on Laravel's printable trade which is awesome. Check this out. This is really cool. This card model here, I will add a use statement for the trade called MassPrun able. It allows us to define a function called Prunable as Copilot has understood and
able. It allows us to define a function called Prunable as Copilot has understood and here we can return a query builder. So I'll accept that we want to retrieve the card instances where the updated ad field is lower or equal to seven days ago. This code right there says that if any card has an updated ad field which is older than seven days, it should be deemed Prunable and be removed whenever there's the
older than seven days, it should be deemed Prunable and be removed whenever there's the next chance to do so. Now, I don't even want to keep the card for seven days, I'll be much more aggressive. You get to choose what you want, but I'll go sub hours and set two. So I'll keep the cards for two hours. This by itself is not going to prune and delete the cards, but it is going to make
Testing prune command1:50
This by itself is not going to prune and delete the cards, but it is going to make it eligible to be pruned when we run a specific command. All right, check this out. I'm going to write a comment, PHP, Addison, Model, Prune, but I'll add the pretend flag. So we're just going to play pretend and see if Laravel finds any model that should be pruned according to our Prunable trade setup. I'll press Enter and it finds no records here.
pruned according to our Prunable trade setup. I'll press Enter and it finds no records here. I was hoping and assuming it would find two records because I have two cards that are definitely more than two hours old, but I know why it's not finding them. The reason if we look at our database is I did not feel the updated ad field when we created the cards manually, but let's be cheeky here and I will copy that. And for these two, I will paste it, but go back a couple of days. So the sixth of December and here we'll do the same.
And for these two, I will paste it, but go back a couple of days. So the sixth of December and here we'll do the same. Let's do this one the fifth of December. So now the first two cards are clearly more than two hours old. And so I will rerun the pretend command. And there we go. This time it has found two cards that will be pruned whenever we run the command without the pretend flag. Well, let's do that.
the pretend flag. Well, let's do that. Let's get rid of the pretend flag and run the command. And it looks like it has pruned two cards. If I refresh the database, we should just have the cards with an ID of three left and we do. I still had to manually run this command in the terminal for this to happen. What are we schedule a task that will run every day and take care of that for us?
Scheduling daily pruning3:18
What are we schedule a task that will run every day and take care of that for us? If I go in routes slash console that PHP, I can use this file to schedule my pr une task that I did manually check this out schedule, which I can import from the support facades and then command and he will have a model prune command and we will run this daily. Of course, as you can imagine, we could run this hourly here. Let's go back to daily.
Of course, as you can imagine, we could run this hourly here. Let's go back to daily. And that's done. Now Laravel is going to run these comments every day and prune out all the cards that haven't been updated for at least one hour. Now we still have one little problem with that. I want you to pay attention to the cart idea of three, which has an updated at date of the 8th of December at one 13 and 37 seconds.
Touching cart on item changes4:08
date of the 8th of December at one 13 and 37 seconds. This actually blew my mind how cool it is. So I will go in the cart item model because after all we interface with the cart item when we add remove items from the cart and all I have to do is add another protected property called touches, which is also an array. We want the cart model. That's it with just that whenever we interface with the cart item, the touches
We want the cart model. That's it with just that whenever we interface with the cart item, the touches property will signify, hey, you have touched the cart and therefore I need you to update the updated at property on the cart instance. How cool is that? So now check this out. We are still at one 13, 37, but I will go, but I will go and add yet another dessert
We are still at one 13, 37, but I will go, but I will go and add yet another dessert to our cart, the red velvet cake. Click on it. It's been added here and this time when I refresh the database, the cart instance here should have its updated at field updated ready. Here we go. It has worked. It's now 204 32, which matches the creation of the last cart item.
It has worked. It's now 204 32, which matches the creation of the last cart item. Come on now. How cool is that? That means that every time a visitor adds or remove an item from their cart, it 's going to reset the two hour window that makes us keep the cart in the database. So if they wanted to buy something and then they came an hour and a half later, added one more item and then everything gets swiped, it's probably a bit frustrating
Previewing cart UI work5:31
added one more item and then everything gets swiped, it's probably a bit frustrating for them. So we reset the timer every time there's an interaction with the cart through cart items. Super cool stuff, right? All right. I think we are due for some tailwind CSS in the next lesson. We're going to work on the cart UI this time with items and probably do the ads remove increment
We're going to work on the cart UI this time with items and probably do the ads remove increment decrement button thing on the product itself. See you there. I'm looking forward to that one.
