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.
18 episodes1 hr 40 mins

Overview

Build a drop-in comment system with Livewire that instantly works for any model.

We’ll cover top-level comments and replies by re-using Livewire components, editing and deleting comments, working with Alpine.js to minimise network requests, building an Alpine.js directive to display when a comment was posted, handling deleted users and loading more comments gradually.

Once you’re done, you can drop a single Livewire comments component wherever you need it — and comments will instantly be enabled.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

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