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.
6 episodes 21 mins

Overview

Let’s skip the database and build the ability to like any model in Laravel, using Redis.

Traditionally you’d reach for the database for this kind of thing, but as you load more models and start performing checks within relationships — things begin to slow down.

With a key-value store like Redis, tracking users who have liked comments (or anything) keeps everything ridiculously fast.

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

Episode discussion

No comments, yet. Be the first!