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
04. Showing if a user has liked something

Transcript

00:00
Before we can allow a user to remove a like, we kind of need to know whether they've liked something. So let's focus on that first of all. OK, let's go ahead and create out the method
00:10
that we're eventually going to use here. And we'll say liked by, and we'll pull in the user here so we can check if they've liked it. Now, in Redis, we have a return value here now
00:21
because we actually need to know whether this is a member of a set. And we use the S is member method to go ahead and figure this out. So again, this is going to be exactly the same thing. We'll tidy this up in a minute because it's
00:36
a little bit annoying to have to duplicate this. And then we're just going to pass the user ID in. So this will basically check if the ID exists within that set, pretty straightforward.
00:45
OK, so we can hop over to our index here, and we can show now whether it's been liked by a user. So again, we'll just keep this super simple. And let's say if comment liked by, and we'll
00:59
pass the currently authenticated user in here, then we'll say something like you liked this. OK, let's hop over and check this out. And yeah, sure enough, I've liked every single one of these.
01:11
OK, before we go any further, let's hop back over to our comment and just look at the duplication that we have here. This isn't the ideal situation. We wanted to change the key around at some point.
01:22
It's going to have to be updated in multiple places. So let's go ahead and create out a protected getLikesKey method in here, which returns to us exactly this. So we can go ahead and just reuse this.
01:36
So let's go ahead and pull this in like that. And let's go ahead and reference this in every single one of these. So this getLikesKey, and we'll do the same thing
01:46
for all of these as well. getLikesKey, and the same here as well. OK, great. So this shouldn't change at all, of course,
01:58
because we're just referencing the same thing. And this is also going to make it super easy to update later when we want to apply this in a trait to potentially any model. So we can just really quickly update this method.
02:11
OK, so we can now see who has liked a comment or if we have liked a comment. Let's go ahead and look in the next episode at removing likes.

Episode summary

In this episode, we dive into figuring out if a user has already liked something, which is essential before allowing them to remove their like later on.

We start by creating a handy method (likedBy) that checks whether a given user has liked a particular item (like a comment) using Redis’s SISMEMBER method. This lets us see if the user’s ID exists in the set of IDs that have liked something.

Then, we update our UI so, for example, it can show a message like "You liked this" next to the relevant comments. This makes the experience more interactive and personal.

Next up, we tidy the code by centralizing the likes key logic into a dedicated getLikesKey method. This avoids repeating ourselves, making the code easier to maintain and update later—like when we want to apply this logic to other models using a trait.

With that in place, we can accurately see who’s liked each comment and prep for the next episode, where we’ll tackle removing likes!

Episode discussion

No comments, yet. Be the first!