Playing
01. Trending Things Over Time With Laravel

Episodes

0%
Your progress
  • Total: 7m
  • Played: 0m
  • Remaining: 7m
Join or sign in to track your progress

Transcript

00:00
In this snippet, I'm going to show you a really simple and effective way to create a list of trending content or trending things
00:08
over time in your Laravel applications. So what I mean by that is, let's say we have a list of articles on the page. Just create a really simple controller here and a view
00:19
and list out these articles in no particular order. What we want to do is, as we click on each of these articles, we want to go ahead and increment some sort of trend score.
00:29
Then what we want to do is schedule a task in the background to decay these over time. And that's effectively all we need to create a consistent trending list of models
00:40
in our application. OK, let's go ahead and jump in and see how we can do this. OK, so the first thing is, we've got our articles here. Let's just take a look at the index page that's
00:51
listing through these. Really, really straightforward. And when we click on that link, it goes over to this route just here, which shows the article.
00:59
Pretty straightforward stuff. OK, so what we want to do is, first of all, go ahead and create out a migration. So let's make a migration here.
01:07
And let's go ahead and add in a trend score to the articles table. So let's go ahead and add this in to articles table. And let's open this up.
01:16
So add trend score. OK, so this is going to be a simple float value. So let's say float. We'll go ahead and call this trend score.
01:26
And let's set a default here, really importantly, to 0. So when anything gets created, it starts off at 0. So let's go ahead and migrate this. And yeah, sure enough, we've now got a trend score in here.
01:38
What we can now do is head over to our web routes. And we can order the articles by this trend score. So let's go ahead and order this by trend score in descending order.
01:49
So we get the highest trending at the top. And if we head over, of course, we have no change at all. So what we're going to do is whenever something in your application is viewed
02:00
you want to show as trending, we're really simply just going to increment that column. Now, that's not going to do much. It's always going to push the most view to the top.
02:08
And it's never going to decay over time. But let's do that first of all. So let's take the article. And let's just use the increment method here
02:17
to increment that trend score. And just so we can keep an eye on this, I'm going to come over and output the trend score on the page.
02:27
OK, there we go. So we've got zero for all of these. Let's click on the first one. And of course, you can see that increment.
02:34
So this is working. But the only issue is if we have an article that is viewed a bunch of times but then isn't viewed for a while, this is always going to keep this high score.
02:47
And it's never going to decay over time. That is where our simple command comes in, where we're going to go ahead and start to reduce this value. So we're going to go over and create out a command for this.
03:00
So let's make out a command. And we'll just call this Adjust Trend Scores. So let's go over to Adjust Trend Scores. And it would be good if we could batch update this.
03:10
But for now, let's just keep things really simple and just iterate over each of our articles and decay this. So let's go ahead and fetch out all of our articles. And we're going to go and each over these.
03:21
So we'll just iterate over each of these articles. Of course, we'll get an article back just here. And all we're going to do with this is go ahead and update this.
03:30
So I'm going to update this quietly because I don't want this to affect any observers that I have. That's really important.
03:36
And we're going to update the trend score by taking the trend score. And we're going to multiply this by a value under 1, so something like 0.97, something like that.
03:50
You can adjust these. And I'll talk about how that affects this in a minute. OK, so now that we've created this command, this just has the default name.
03:57
So let's grab this and go ahead and run this command. There we go. And let's head over and give that a refresh. And you can see that that has been reduced.
04:06
So what is going to happen now is if we schedule this to run, say, every minute or every five minutes, probably not a good idea to do this every minute unless you're batch updating.
04:15
This is just going to, over time, decrease this. So let's run this a couple more times. And let's head back over. So now when I click on this one, that's
04:25
going to increment the score. But if this one hasn't had many views for a while, this will continue to decay. And then this one will gradually catch up with it.
04:36
So for example, if Article 2 gets viewed a couple of times, of course, in between there, the trend score is still going to be reduced. But that means that if Article 1 hasn't
04:46
been viewed for a while, eventually, we're going to get to the point where this starts to overtake it. So obviously, it's going to overtake it at this point. But the next time this gets run, you can see it will bump down.
04:57
So let's do that a couple more times because we're assuming this is happening every five minutes or so. And you'll notice that that will more quickly overtake it
05:07
as that value decays over time. So this is a really common pattern for doing this. There are lots of other ways that you can do this without running a scheduled command.
05:17
But I've used this really successfully. And it works really, really nicely for training content. OK, so what we're going to do now is look at scheduling this. And then I'm going to talk a little bit about how adjusting
05:27
values makes this better. OK, so let's go and just take the command name here. Let's come over to our console routes. OK, so let's go ahead and bring in our schedule facade.
05:40
And we'll reference that command. And let's say that we want this to happen every second, just as an example. So now that I've done this, I can go ahead and either run
05:50
phpr to schedule work on my local machine, which will do this every minute. So it's not quite going to catch every second. Or we could say schedule run, which is going to run
05:59
but eventually close off. So when we run this, you'll see that these scores are just gradually decreasing. And when we click on Article 1, that bumps to the top.
06:07
Article 2, meanwhile, is just continually decaying to the point that it doesn't become trending anymore. Now, you can adjust this in lots of different ways.
06:15
So for example, every second, obviously, with the method that we've used here, even with batch updating, is going to be really intensive. So what I would do is typically schedule this
06:25
for, let's say, every half an hour. So if we come over to Adjust Trend Scores, the higher this value is, the faster it's going to decay. And the lower this value is, the slower it is going to decay.
06:36
So for example, if I set this to 0.20, and we just hop over to the database and just reset these, and we come over, let's click on Article 1 a few times.
06:48
And let's do Article 2 once. Let's go ahead and rerun our Adjust Trend Scores command. And you can see that already what's going to happen is that's going to pretty much overtake it much more quickly.
07:00
So that's one thing that you can adjust. But typically, I leave this at about 0.797. And that gives it a nice, steady decay over time. The other thing that you can do is adjust how frequently you
07:13
actually run this command. And that's going to give you a more up-to-date value as this is viewed more often. So as each of your models is viewed more often,
07:23
the faster that you run this, the more real-time trending list you're going to see. And that is pretty much it. It seems really simple, but I've used this really effectively
07:32
to show a list of trending articles on the CoCourse website, and it works really nicely. You might just want to tweak around the values as you go just to experiment with how fast things decay
07:45
and how much of a real-time feel you want to this. So that's it. That is a really simple way to look at a trending list of items within your Laravel apps.
1 episode 7 mins

Overview

Building a trending list of content with Laravel is actually pretty easy. In this snippet, we’ll take a look at how to create a list of trending articles over time, gradually decaying less viewed articles and pushing popular articles to the top.

You’ll also be able to adjust the decay levels, and update the frequency of decay to tweak how sensitive your trending content is and how ‘realtime’ it appears.

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

Episode discussion

No comments, yet. Be the first!