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.
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!

Comments

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