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
06. Liking any model using a trait

Transcript

00:00
So there's one glaring issue here, and that is that we've tied all of this functionality specifically down to our comments.
00:08
What if we want to apply this to articles or courses or any other model that we have in our application? Well, for that, we're going to go and create out a likable trait, which will allow this functionality
00:21
to work for any model as soon as we apply it to that model. OK, so we'll start by just grabbing pretty much everything that we have inside of here. So let's just take all of this, including the getLikes key.
00:33
That's really important. And we'll go ahead and create out a trait. So let's pop this directly in our models directory. So let's create out a traits directory in here.
00:42
And then let's create out a class in here called likable. And we'll just swap this over to a trait and make sure we spell that correctly. OK, great.
00:52
So let's create this trait out. And we're just going to paste all of the methods into here that we created previously. Now, you'll probably notice that comments is, of course,
01:03
not going to be appropriate for this. If we did this and shared it between multiple models, all of the likes would get mixed up. So what we're going to do instead
01:12
is go and use something like sprintf. We don't need to do this. We could use concatenation if we wanted to. We're going to take a string here for the model name.
01:21
We're going to take a string here for the ID of that particular model. And then we're going to say likes. So exactly the same structure as we had before.
01:29
We're just going to replace these elements. Now, to get the table name of this thing that we've applied it to, the model that we've applied it to, we just use get table.
01:39
And then we just want to take in the ID of the thing. So if we applied this to the comments model, or the comment model and that had a table name of comments, this would be comments.
01:50
And then the comment ID, likes. We applied it to articles. It would be articles. So now we can very confidently just add this
01:57
over to any of the models, and the key will automatically change. So let's go ahead and try that out. If we head over to our comment and use
02:05
that likable trait in here, let's see what happens. OK, over to the browser and give that a refresh. And yeah, sure enough, it works in exactly the same way. And you can see that this still picks up on the correct key.
02:17
I'm not going to do this now because it's going to involve creating out a new model, listing out new data. But if you did now create out a new model, all you need to do
02:25
is apply this likable trait to that, and it will go ahead and pick up on this for you, just because we are now using a dynamic table name for the Redis key.
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.