This episode is for members only

Sign up to access "Build a Livewire Comment System" right now.

Get started
Already a member? Sign in to continue
Playing
17. Handling deleted users

Transcript

00:00
Let's tackle what happens when a user deletes their own account. Now, if we go ahead and cast our mind back to when we created our comments table, remember that we set the foreign ID for the user to nullable, and then we set this to null on delete.
00:17
That's absolutely fine. But what's not going to happen is it's not going to go ahead and set the deleted app date and time. So let's go ahead, create a comment, delete our account and see what happens.
00:30
And then of course, implement the solution to fix it up. Okay. So I'm going to go ahead and just post a new comment. I've cleared everything out of the database here, and let's go ahead and
00:39
just reply to this and say a reply just to make sure that it's working for all of these as well, and we'll just do something else. So we've got a few comments from this user. Let's go over and delete my account.
00:53
So I'm going to head over to my profile section and specifically within Breeze, we can just delete our account like this. And there we go. That's been deleted.
01:01
When we head over now, though, we've got a bit of an issue. We can see that we've got call to member function avatar on null. Let's open the database. And you can see that what's happened is we have set the user ID to null.
01:13
That's done at the database level, but the deleted app column is still null. So it's still trying to show the user for each of them comments. Okay. Let's go ahead and clear this out again by just truncating the whole table and
01:27
we'll go ahead and restart everything. Okay. Let's go back over here and try this again. So I've deleted my account.
01:34
So I'm going to need to go ahead and register this just one more time. And once we're in and authenticated, I'm going to post a couple of comments here. Let's just do another reply and we'll just do one more top level comment. Okay.
01:50
So how do we fix this up? Well, we're going to head over to the user model and when the event comes through from eloquent that the model, e.g. the user is trying to be deleted.
02:01
We're going to do a little bit of clear up and delete them comments and soft delete them. So how do we do this? Well, we can create an observer class for this, but let's just do this
02:10
directly within the model for now. So we're going to have a public static function called booted. So this is going to happen when this model gets booted up from here. So what we can do is we can attach any of the events using eloquent.
02:24
So we can say something like when this is deleting, not when it's physically deleted, because then we won't have access to the model while it's deleting. We're going to get an instance of that user in here, and then we can do something with that user.
02:39
So what we want to do is something like user comments and delete, and that will delete all of the user's comments, at least soft delete them unless we use forced delete. So we'd have a relationship for the comments just yet, but
02:52
that's a pretty simple has many. So let's go ahead and create this out. Now has many and common. There we go.
03:00
So now as this user has been deleted or more appropriately before this user gets deleted, we clear up and delete the comments first of all. We're not going to have that user ID hanging around because that user won't exist anymore.
03:14
So there's no point, but these will also be soft deleted. Okay. Now that we've done this, let's try it out. So I'm going to head back over to my dashboard and let's try and
03:24
delete our account this time. Enter our password and our accounts deleted. And now, sure enough, when we come over, all of them comments posted by that user who has deleted their account have been soft deleted in the database.

Episode summary

In this episode, we deal with a common problem: what should happen with a user's comments when they delete their own account? First, we look at how things are set up right now—when a user is deleted, the user_id on their comments is set to null, but the comments themselves stick around and aren’t marked as deleted. This causes some issues when the app later tries to load details like the user's avatar for those comments, leading to errors.

To demonstrate the problem, we create some comments, delete the user, and see firsthand what goes wrong. After clearing the database and going through the user account deletion again, we set out to fix things up properly.

We jump into the User model and use Eloquent's model events to hook into the deletion process, setting up a booted method. Here, before the user record is actually deleted, we find all the user’s comments and soft-delete them (instead of just orphaning them or wiping them out). This cleanup makes sure that when a user goes, their comments are safely marked as deleted, keeping the database tidy and the app from running into null pointer errors.

Finally, we test this new behavior by creating a user with comments, deleting the account, and confirming that the user’s comments are indeed all soft deleted. Problem solved!

Episode discussion

No comments, yet. Be the first!