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
05. Cleanup and other tasks before pruning

Transcript

00:00
So we've implemented a fairly common very simple condition under which we need to prune but in reality your application is going to be a bit larger and you might need to do a little bit of clear up as you are pruning models. A good example of this are relationships that are
00:16
attached to this so for example if we had a deployment log table we'd probably want to clear them up as we were deleting this or we'd probably get an error or if you had some sort of file model where you were uploading files either local or to a third-party service you'd probably as you were cleaning up these records in your application want to delete the physical files that were elsewhere
00:39
so you can pretty much roll with this however you want we're going to create a really simple example here and we're going to go ahead and create out a model just called deployment log and we're going to generate a migration and a factory alongside of that so let's open up the create deployment logs table again we'll keep this really simple and all we need to include in here
00:58
is a foreign id that points to the deployment so let's just pop deployment id in here and that's pretty much all we need for now we don't need to over complicate this so let's go ahead and run phpr to migrate and there we go great so let's set up the relationship over in our deployment model really quickly so let's just call this deployment logs and this just has many deployment
01:22
logs then we'll just manually create a couple of records and see these deleted alongside so we're going to say has many deployment log like so okay so what we're going to do first of all is create out some deployment logs for deployments that are due to be deleted so we can just say record ten thousand let's go ahead and pop that in there so ten thousand we know that's going to be deleted
01:45
because that's a very old one and let's see what happens so i'm going to go over to here and say php artisan and model and prune and let's see what we get now if we go run through all of this we actually get an error here now the reason that we get an error is because we have a foreign key constraint here that meant that the deployment with an id of ten thousand couldn't be deleted if
02:08
we head down here you can see that's still in there now that's not what we want of course you can handle this at the database level by using a cascade and you can delete this if the related record is deleted but if you're handling this from your application which i tend to do tends to give you a little bit more control then you would want to manually clear these up first so
02:28
i'm just going to go ahead and get rid of in fact let's just keep that in there because we know that that one's going to be deleted anyway we can leave it as it is and let's go ahead and implement the ability to do a little bit of cleanup here so to do this it's just as easy as the pruneable method and this is a method called pruning so if this exists it's going to be invoked as these are
02:50
pruned and once again we can do pretty much anything we need inside of here delete files delete relationships anything that we need to do so in our case we're just going to reference the deployment logs relationship for that particular model and delete them that's it that's all we need to do so let's go ahead and rerun model prune you can see that we get one pruned that
03:10
previously failed that's deleted the deployment log and of course it's deleted that main deployment in here as well so whatever you're doing in your application and whatever kind of cleanup you need to do you now know that you can implement this pruning method to go ahead and do cleanup as your records are pruned
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!