In this episode, we're tackling how to let users delete comments—without having to reload the whole page. We'll go step-by-step:
First, we add a delete button to each comment (but only if the user is allowed to delete it). We tighten up our comment policy so only comment owners can actually delete their own stuff.
Next, we wire things up using Alpine.js to show a simple browser confirmation before running the delete (so users don't accidentally delete stuff). If the user clicks 'yes', we call a Livewire method to actually handle the deletion.
Then, we focus on not removing the comment from the database completely. Instead, we use Laravel's soft deletes feature, so the comment's just hidden (deleted_at gets set) and not permanently wiped away.
Finally, we update the UI so the deleted comment automatically disappears from the list, without needing a page refresh. We do this by toggling a local boolean (deleted
) and wrapping the comment markup in an if-statement. There's a little talk about potential UI quirks (like borders), but those are just minor fixes.
By the end, you've got proper comment deletion set up: authorized, confirmed, soft deleted in the database, and instantly hidden on the page. Nice!