YouTubevideo· Jul 16, 2024· 14:32· 2,000 views on YouTube

You're missing out on using Soft Deletes in Laravel.

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

37 moments
  1. There's a lot of niceties that Laravel has out of the box that allow you to build applications faster. One of those is soft deletes within your models. So, why don't we take a look? Let's say you had a list, in this case maybe a list of submissions, whether that's bug submissions from your users or maybe from your employees, for example. And you wanted to, you know, modify that list. Well, a lot of the times, when you have a any delete function within your application, most of the times you know that that is deleting it completely.

  2. So, if I was to delete, you know, this specific submission right here, delete it, it's gone. It's it's forever gone. It's deleted from the database. There's no way to retrieve it. Well, there's a lot of ways to kind of combat this or to fix this. You could, when you click delete, maybe have a pop-up and you can say, "Are you sure you want to delete it? Type yes or no." That kind of thing.

  3. Or it could just be a confirmation dialogue. And that's great, but you're still having this process of there's no backups. If you have a database backup and these are important records, you could probably like, you know, re-migrate the database and get everything back that way.

  4. And then you could go the more custom route. You could say, "Well, when I'm deleting this, what I actually want is to set a specific, you know, column on this particular record that I clicked delete for. So, now it's not actually deleted. I'm just not showing it to the user."

  5. But, there's a way to do this out of the box that Laravel's giving you and it's incredibly easy. So, why don't we set it up right now. If we were to take a look at the Eloquent uh documentation, there's a whole section on soft deleting. So, in addition to actually removing records from your database, Eloquent can also soft delete models. When models are soft deleted, they're not actually removed from your database. Instead, a deleted at attribute is set on the model indicating the date and time at which the model was deleted.

  6. Great. So, that's basically doing that custom setup that I said, except Laravel just gives it to you out of the box in a very eloquent way of doing it. So, let's do this. Use soft deletes. So, let's go ahead and add that to our model. So, I have this submission model and I'm just want to say use soft deletes. We'll bring that in.

  7. And now, the only thing we have to do is create a new migration for this model because we need that deleted that column. And again, Laravel makes this incredibly easy because you can just have this table soft deletes. So, why don't we go ahead and do that? So, um I'm just going to say, let's go PHP artisan make migration and we'll say add um deleted at column to uh what is this?

  8. Submissions s table and we'll pass this a model of or I believe you have to pass it a table. So, table of submissions. I think that's correct. Let's take a look. Submission. So, add deleted at column. And yes, table of submissions. Perfect. So, we'll what we'll do is we'll add the soft deletes. And then we'll go ahead and drop soft deletes. So, simple as that. Now, let's go ahead and migrate this.

  9. So, PHP artisan migrate. Perfect. So, now we have this deleted at column in our submissions model in our submissions table. Um so, why don't we take a look at what this looks like now. So, uh looks like everything should be good to go and let's go ahead. And so, now automatically, now we don't have to change our query, change our In this case, what I'm doing is I am having a uh let's go ahead and go to my list submissions Livewire component.

  10. I am having a delete submission where we just simply find that submission based off the submission ID and then just call the delete function. I don't have to change this, which is awesome. It will just automatically work because we're using soft deletes now.

  11. So, why don't I Let's go ahead and let's migrate. Let's uh Let's go ahead and let's delete some more some more things. Okay. So, we let's delete this one, this one. We'll just delete a bunch of them. Okay. Now, why don't we try and show those now deleted submissions?

  12. Cuz maybe What if we wanted to, you know, every 30 days or so, maybe delete all of them, or just put them in a trash file, a trash part of our website so that the user can delete them manually. Well, let's see what we can do within the documentation.

  13. So, first of all, now we can say So, we can restore soft deleted models by using the flight restore. Okay, that's helpful. And then, um we can determine if one's actually been trashed using this trashed method. And we can permanently delete models using force delete. Okay, so that's helpful as well.

  14. Um perfect. So, now we can query these soft deleted models. So, you can also say, "Okay, I want to show all the soft deleted models within a loop." You can use that with the with trash. But what I want to do is do only trashed. So, let's go ahead and do that. So, maybe let's go ahead and in the nav, um what we're going to do, let's go ahead and response not this one.

  15. Let's go ahead and then our navigation, what we want is another link here. Then maybe this will go to the trash. Let's just call it that, trash. Uh Uh And we'll say trash here. And Okay, and then our web routes, we're going to we'll just duplicate this. We'll go into trash.

  16. And why don't in our dashboard, we'll copy all of this into our trash. Oh, duplicate this. There we go. So, we'll duplicate this. We'll call it trash.blade.php. Perfect. And now we'll go ahead and create a new Livewire. So, make vault. We'll say that this is uh trash submissions.

  17. Perfect. Now, we can call this here trash submissions. Perfect. And why don't I call this trash? All right, let's see what this looks like. Route trash is not defined. Aha, that's because I did not name it properly. There we go. Okay, so now we have trash. And nothing's here yet. So, why don't we start listing out the trashed options?

  18. Again, what we see here in the eloquent is that we can call only trashed on our model and actually get that. So, let's go ahead and in our trash submissions, we can say public function with and we'll say we'll return the uh submissions only trashed get. Perfect. And why don't we um Well, that'll be good for now.

  19. So, what what we'll do is we'll go ahead and let's just I'm just going to let uh this kind of like loop through them. So, what we'll do is we'll say for each submissions as submission and we don't need this. All we want to do is let's just show uh submission submitter.

  20. And we'll show the limits. Perfect. Or it's a the description, I should say. And well, that should be good enough. And I don't need a for each. I think I need one more div here and we should Nope. That should be good to go. And let's take a look. Nope. Let's see. Here we go. And there we go. That should be good.

  21. Class submission not found. Let's pull that in. So, use that model submission. Aha, that's why. We don't need this for each here. We need this for each there. There we go. Okay. It doesn't look that great. Because we will just delete this flex. There we go.

  22. So, you can see here. These are all the submissions that were deleted. If I was to go ahead and delete some more, we should see some more in the trash now. Perfect. So, you can see how this would be incredibly useful if you're building application and you don't necessarily want things to be deleted right out the gate, even with a modal.

  23. Let's say we added a modal here where you couldn't delete it until you approved it within the modal. But now you also have this separate, you know, separate uh backup, I guess would be a great way of putting it, or just a a second-hand defense to make sure that your users aren't actually deleting things that you're going to have to go into the database, like if they you know, email support, you're going to have to go into the database, you're going to have to get the backup, and then you know, restore it for that particular user. This is just one extra line of defense.

  24. Well, there's other things that we can do, like here. Um what we can do is if we needed to force delete. So, why don't we just add a quick, you know, maybe a quick uh button. And we'll just say um We'll say So, there's a restore button that we could use, and then maybe we'll add another button of delete. There we go. So, we can click wire click restore submission, then we'll have wire click delete submission.

  25. Perfect. And so, then we'll have delete submission with the submission ID, and we can go ahead and this would be the force delete. So, we have here force delete. Perfect. And then we also have the ability to restore a submission. And this is perfect to be able to say, "Okay, maybe you want to, you know, restore a submission from the trash in this in this particular instance."

  26. So, um we have restore. Oh. And I don't think it's actually called restore now now I'm thinking about it. Let's see. So, we have force delete, pruning models. Let's see what what it is again. Oh, history restore. So, the restore method may also be uh restore. Should be a good to go. Sometimes you may wish to undelete a soft delete model restore method. Okay.

  27. Submission find restore. Why does that not work? Uh it's because we can't actually find the ID. So, we have to say let's use the restore.

  28. So, we can technically pass in the whole model to restore it. But in this case, if we're having a list of particular soft deleted models, then we have to call with trashed first because this submission find does not include the with trashed. So, we can say submission with trashed find. And we'll do this for as well. So, with trashed find. There we go.

  29. And restore. There we go. Restore restore. And we'll delete these. So, Jewel Crist, we'll restore that. Look back, Jewel Crest, perfect. Delete. And restore. You can see here.

  30. Okay, great. Now, what about automatically pruning? So, this is a great use case that most applications have where you have this, trash bin. You see it in every application, whether that's Google or Dropbox or whatever you have you. You usually delete something and it's not necessarily deleted. It's the trash and then you can empty that, you know, or just empties itself automatically every 30 days.

  31. Laravel's already got that accounted for, so this is the pruning model. So, we can say, you know, we can use this trait in our model, use prunable.

  32. You know what? We may want to periodically delete models that are no longer needed. And in this case, we could say we want to prune the models that have been that have this deleted at uh table now, deleted at column within the database, the ones that are effectively soft deleted. And then we just want to delete them.

  33. And we can call a schedule to delete them daily if we want. And pruning models isn't necessarily just for soft deletes. This is another feature that it would be great to go into completely in a new video, but pruning models is great for when you have models that you just want to consistently get rid of them.

  34. Maybe you have like a premium plan and your users can only have a specific set of history or something like that. It's just a great way to naturally, without having to write all of the, you know, eloquent queries yourself, say, "Okay, every time, in this case, the created at is between now and a sub month, then we'll just prune those models across the entirety of your application."

  35. But, you could use prunable as well for soft deletes where you could say if things have been in the soft delete status for 30 days, why don't we prune them? So, soft deleting is a great way to be able to use the tools, the helpers, the functionality Laravel provides out of the box, but use it in a way that most applications already do without having to actually manually create all of that by hand.

  36. So, maybe you can think of some ways where you're like, "Hey, I need soft deletes cuz I don't want to give this power users to delete all the time, or maybe you just want to give your users an extra, you know, little bit of backup, little bit of peace of mind to say, "Yeah, you can actually just go into your trash folder and do it." Or maybe you don't even want to give your users that ability themselves to restore things, but then you have yourself this piece of mind to say, "Okay, I can actually go into the database and restore them for your user."

  37. So, hopefully that was helpful just to get your mind around new ways that you might be able to better your applications, better your products for your users. So, keep creating.

just you here