This episode is for members only

Sign up to access "Liking Things in Laravel with Redis" right now.

Get started
Already a member? Sign in to continue
Playing
05. Removing likes

Transcript

00:00
OK, I'm going to go ahead and duplicate the route that we already have here to like something. And I'm going to switch this over to a delete route.
00:07
We still want to take in the comment just inside of here. And we want to remove it for that particular user. So of course, we're going to call this something like remove like. And let's go ahead and change the route name over to destroy.
00:20
Now of course, this doesn't exist at the moment. But we can head over to our comment model and add this in. So let's do this just below our add like. So let's create out a remove like method.
00:31
And again, that's going to be for that particular user. Now to do this, we're going to go ahead and use Redis, of course, once again. And this time, we want to remove this from the set. So we want to use srem.
00:43
So again, let's reference our get likes key. And let's pass in that user ID directly to there. So basically, it's just going to remove that particular ID from that set that we've created.
00:57
OK, so now that we've done that, let's hook this up inside of our index. Now once again, I'm working with a really basic blade form here. But if you're working with something like Livewire, Inertia, or anything else, then this is going to be a little bit easier.
01:11
So what I'm going to do is I'm going to take this comment liked by method. And I'm just going to create a ternary here to use our new route if that is the case. And we're going to post that through to the destroy route instead.
01:25
So we're going to say comments, likes, and destroy, passing through that comment. And then because we have a form with a method of post, I'm just going to add another if statement just
01:35
underneath our cross-site request forgery protection to switch over the spoofed method. So again, I'm going to say if comment is liked by user, then we're going to use the method spoofing directive in here.
01:46
And we're just going to pass in delete. And we should be good. So if a user has liked a comment when they submit this form again, it's going to go through to destroy that.
01:56
And it's going to send the spoofed delete method. Let's go over and see if this works. OK, so if I click this, yeah, sure enough, there we go. And I can re-like it again if I need to.
02:08
So the implementation that you work with is, of course, going to change depending on what stack you're working with. But the basic concept is the same. We want to remove a like if we have already liked something.

Episode summary

In this episode, we're focusing on how to remove likes from comments. First, we start by duplicating our existing route for liking a comment, then switch it over to use the DELETE HTTP method for removing a like. We update the route name to something appropriate, like destroy, and adjust the controller logic accordingly.

Next, we jump into our Comment model, and add a new method called removeLike, which leverages Redis—specifically the srem command—to remove the user's ID from the set of users who have liked the comment. It's basically the opposite of the add like functionality!

Then, we update our front-end Blade form so that if a user has already liked a comment, submitting the form will hit the destroy route and use method spoofing to send a DELETE request. That way, the form handles both liking and unliking in a really neat and conditional way.

Finally, we do a quick test to make sure everything works as expected. We see that clicking the button removes a like, and we can re-like as needed. The overall approach is pretty flexible, and although you might use different stacks like Livewire or Inertia, the general concept stays the same—make it easy to remove a like if one already exists.

Episode discussion

No comments, yet. Be the first!