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
03. Pruning models

Transcript

00:00
So to actually implement the prunable functionality within our model is incredibly straightforward.
00:05
We've already defined out the condition that we want within that custom command that we created. So let's hop over to our just deployment model and let's see what we need to do here. So the first thing and of course really important thing is to include the prunable trait from just here. Now that's important because by default we don't really need this functionality on models,
00:27
so it's kind of an opt-in feature. The next thing to do is define a single method in its most basic form to go ahead and give us the condition at which these should be prunable. So pretty much exactly this condition that we saw here. So let's go ahead and create out this method. We want to go ahead and call prunable here or define this prunable method. This is going to
00:49
return to us a query builder. So let's just go ahead and pull that in and from here we're going to return static and where so we're referencing this model where created at is greater or less than or equal to now and of course we want to go ahead and sub out a week there. That is pretty much all we need to do. So what's now going to happen is when we go over to the command line
01:17
and we run php artisan model and prune that's going to go ahead and look by default in the models directory. So if this is slightly different you will need to go ahead and specifically define where you want these to run and we'll take a look at them options in a second but doing this we'll just go through all of your models detect if you have prunable functionality added in there and it
01:41
will go ahead and prune these for you. So just before we run this let's head over and make sure we have those 10,000 records and let's hit model and prune. So that's going to go through in batches so we're just going through each one each batch here and deleting these as we need. So this is a much faster solution. Now if we head over here and give this a refresh there we go we already saw this
02:01
from the introduction but we now have pruned a huge amount of these records. So let's take a look at some of the options that we can pass through here. So we're going to go ahead and say model prune and we're just going to run help and you can see here we've got a few things that we can do. So we can include models so these are just the class names of the models to be pruned we can
02:22
provide multiple ones of this if we want to we can exclude certain ones so if we want to prune everything except a certain model or models we can do that and we can also define the chunk size we already saw that this is being done in sort of batches or chunks so we can increase this or decrease this depending on our needs and really interestingly we can also pretend as well so we
02:44
can do a sort of dry run here to get the same output and show what's going to be removed but not actually remove them physically from the database. So this is really handy if you are creating out a query of course and you want to go ahead and run it just to make sure everything is working nicely before you commit to deleting any data. So you can use them to customize this
03:07
however you want and we're looking at that in a little bit more detail when we get to running these under the scheduler so we'll come back to that a little bit later on as well. So there we go there is our prunable trait really simple and our prunable method which just returns a builder with the condition under which we need to delete.
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!