This episode is for members only

Sign up to access "Laravel Aggregates" right now.

Get started
Already a member? Sign in to continue
Playing
05. Aggregates and soft deleted models

Transcript

00:00
Let's take a deep dive into how aggregates work with soft deleted models. If you've not come across soft deleted models before in Laravel, this is basically just a column over on your database table. So, for example, we're going to add this to comments that says deleted at.
00:16
Now what this will do is it will fill this in with the date that this particular item was deleted and it won't include it in any of Laravel's results unless you ask it to be. So we're going to kind of mix and match this with aliasing and we're going to show, for example, the total comments,
00:32
but then also another aggregate where we see comments that have been deleted. So maybe you wanted to kind of sum up the comments that had been deleted by the user or comments that had been flagged by other people. It could be pretty much anything.
00:46
OK, so to get started with this, we're going to need to go ahead and just add soft deletes to our comments table. So let's make a migration to do this or we could just manually add it. And let's say add soft deletes to comments table. And let's go ahead and open that up.
01:03
So add soft deletes to comments table. And let's just go ahead and add this in. To do this, we've got a handy helper just called soft deletes. And we won't bother with the down migration.
01:13
So let's go ahead and say PHP artisan migrate. And there we go. Now, this isn't going to work straight away. We're going to need to come over to the comment model.
01:20
And we're going to need to use the soft deletes trait on this. That will tell Laravel that this actually does work with soft deletes. OK, if we come over to here and bring this actually back to something a little bit more presentable. So we'll actually output the comments count in our welcome view.
01:39
So let's go and just duplicate this list item. And pretty much just change this over to comments and count. And, of course, change this to comment. If we come over, give that a refresh, we have 50 comments.
01:51
Of course, still one query and a low memory usage because we're making use of aggregates. All right, so we want to exclude from this any soft deleted models. How would we do that? Well, the answer is we don't need to.
02:03
Laravel will automatically do that for you because it treats soft deletes like actually deleted things. So over in here, let's go ahead and just delete one of these. Save that out and come over and see what happens to this count. You can see that it's automatically reduced to 49 because Laravel is taking into account the deleted at column where deleted at is null.
02:24
Great. Now, if we wanted another item in this kind of user stats list to show how many comments had been deleted, we could do that with a combination of aliasing and our constraints. So we're going to keep comments in there, but we're going to add a comments as comments deleted count.
02:45
So that's the name of the thing that we're going to alias it to. And then we're going to add a constraint to this. Let's do this as a short function just to keep things nice and tidy. And in here, of course, we're going to get our query builder.
02:57
Now, with soft deletes, we can actually bring back things that have been trashed or only things that have been trashed, or we can include trashed things. All of that's over on the Laravel docs. But we're going to save for this in particular only trashed.
03:12
So we, for the deleted count, only want to get back comments that have been trashed. So let's take a look at this by just going and dying and dumping on this user. Let's go over, give this a refresh, and there we go. So we've got comment count as 49, not including by default the deleted models, and comments deleted count is 1.
03:36
So we can easily go ahead and just add this to our list of items. So let's say deleted and comment and comments deleted count, and just add that here. And if we come over to get rid of that die dump here, we can see the full stats here. And again, this query is just building up based on what we're adding.
04:01
Now, as an example, just to kind of finish things off, if for the comments you wanted to include comments that had been deleted, this is going to be the place to add it. So let's just have a look at that really, really quickly before we go. So again, we're going to bring in our query builder, and we're going to say query with trashed.
04:20
So that will include any that have been soft deleted. So if we come over, we can see 50 comments. So you can just play around with this. I'm going to go ahead and get rid of this here.
04:31
But of course, depending on what you're building, you're going to want to control this how you need. But that's pretty much how Laravel interacts when it's aggregating with soft deleted models, e.g. it doesn't include them. But then, of course, using an alias here with a constraint to only bring back trashed comments, we can pretty much build up whatever picture we want of this user.

Episode summary

In this episode, we're taking a closer look at how Laravel handles aggregate queries when you're dealing with soft deleted models. If you're new to soft deletes, it's essentially a way to "hide" records—like comments—without actually removing them from the database. We walk through adding a deleted_at column to the comments table, updating the model to use the SoftDeletes trait, and running the migration.

We then update a view to show the total number of comments. Laravel automatically ignores soft deleted records in aggregate functions, so when you delete a comment, the count decreases as expected. Pretty handy!

But what if you want to also show how many comments have been deleted? We dive into using aggregates with custom constraints. By aliasing and tweaking our aggregate, we add a new field that specifically counts only soft deleted comments. This lets you easily build out user stats or admin views showing (for example) active and deleted comment counts side by side—all using efficient queries.

Finally, we touch on including soft deleted models in aggregates with the withTrashed method, so you get full flexibility over your stats. Overall, you'll see how to mix and match aggregates, constraints, and soft delete behavior in Laravel to get just the results you want.

Episode discussion

No comments, yet. Be the first!