This episode is for members only

Sign up to access "Laravel Model Pruning" right now.

Get started
Already a member? Sign in to continue
Playing
04. Scheduling pruning

Transcript

00:00
So now we've implemented our condition to prune this, and we know how to run this with php artisan, model, and prune. You probably don't want to be doing this manually. Chances are you're going to forget,
00:12
or it's going to waste a huge amount of your time by having to come in, SSH into your server, and specifically run model prune. That's probably not what you want to be doing.
00:22
So what we're going to do is head over to the console kernel class, and we're going to define out a scheduled command in here to do this at a certain point in the day. So you've got a couple of options here.
00:33
You can go ahead and schedule this command like this, model prune, and maybe you could do this daily. What we're going to do just to test this out is do this every minute,
00:43
just so the command actually runs when we do this. But in reality, you'd probably want to do this daily, weekly, whatever suits the schedule that you need. So if we just come over here and run php artisan schedule and run,
00:56
you'll see that that goes ahead and runs that schedule command. Of course, because it's running every minute and the cron job minimum is one minute, then that's going to run every minute.
01:06
Like I said, most of the time you're going to want to run this maybe weekly, at weekends, whatever suits your application. Now, that's absolutely fine. But remember, this is going to prune all models.
01:17
You might have specific requirements for each of your models. And that's where the prune help command came in earlier, and we saw that we can define specific models or exclude things. So for example, let's just say that we had two different models.
01:33
We're not actually going to create another model just yet. But let's say that we wanted to prune one weekly and maybe one at the weekend. So what we'd want to do is as a second argument to this,
01:44
start to pass in some artisan options. So let's just pull that down so it's a little bit neater. And that is model. And it can be any of them that are available.
01:53
So now what we want to do is inside of an array here, because this argument takes in an array, we just want to define out the models that we want to prune. So let's say that we want to define deployments to be pruned weekly.
02:08
And we want to define another command in here to go ahead and, if we just pull this in, prune the user model maybe at weekends, like so. Then that would run this weekly, of course.
02:23
And this would run this only at weekends. So we've got two different conditions and two different schedules for these models. And of course, you can include other models in here if you have them.
02:34
It's really up to you how you define this. You might want to exclude certain things at weekends, whatever you need to do. So once you've gone ahead and defined your schedule commands,
02:44
really all you need on your server now is just to have a command to go ahead and run your schedule, probably a cron job that runs every single minute on your server. And then your pruning is completely taken care of automatically.
6 episodes 23 mins

Overview

Database tables filling up? Let's learn how to quickly and automatically remove records with Laravel's native Pruning functionality.

Before Laravel 8.50.0, we'd have to take care of this manually. Now, it's as simple as adding a trait, a query builder, and running an Artisan command.

We'll also dive into what's happening behind the scenes to understand how this works.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!