Playing
02. Before pruning

Transcript

00:00
Before we actually move on to the model pruning functionality built into Laravel, let's just take a quick look at how this would have been done previously without this built-in
00:09
functionality. And this can still be useful if you need to do other things. And this will be usually done with the console command. So let's go ahead and make out a command in here. And let's call this prune deployments. And of course, you can call that whatever you want. Okay, so we now have this prune deployments command, which we can go ahead and register. So let's just call this
00:29
prune and deployments. And that will now be available as an artisan command, we can go ahead and run this. So php artisan prune deployments, there we go, it's run. Now it's not doing anything at the moment, of course. But this is where we would gather all of the data under the certain condition that we would want to prune for. And then we would just delete them
00:47
records and do anything else we needed to do. So let's first of all grab this data. So assign this to a deployments variable. And we're going to go ahead and of course, access the model. And we're going to do this under any condition. Now ours is a pretty simple condition where the created at date is less than or equal to a week. But you can of course, chop and change this around
01:06
based on the data that you're working with. So let's say we're created at and let's say less than or equal, less than or equal to now. And then we want to go ahead and sub a week from this or you could sub a few days or whatever you needed. So now that we've got that condition in here, let's just count them really quickly. And just die dump on the count. And just run that
01:29
command again, just so we can see what we've got. So this is similar to what we saw in the introduction, we've got 9832 that need to be pruned that are old, and we don't need them. So with that in place, what we can now do is let's actually bring in our query method here. So we can just pull this down, it's a little bit easier. And let's get rid of that count. Of course, we're not
01:51
going to use that. We're just going to directly go ahead and delete these. Now bear in mind that this does seem pretty easy to do. But with pruning, this actually works in a slightly different way. We're going to discuss that when we get to it. And you might find it a little bit more efficient to use pruning. So let's go ahead and just run that command again. And we should if we just head
02:11
over here, see 168 rows. So that's gone ahead and pruned all of them older records that are, of course, older than one week. Great. So that's working nicely. Just before we carry on, let's truncate this, restart this. And we can go ahead and just regenerate out all of that data. So we've got that to play with when we actually look at pruning.
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!

Comments

No comments, yet. Be the first to leave a comment.