Playing
02. Logging unique views for models

Transcript

00:00
So we're starting off with a super basic app that we saw from the last episode, where I have a bunch of articles.
00:05
And I can just click through onto each of these. We don't have counts at the moment. That's what we're doing in this episode. So in the database, I have three of these.
00:13
And it's basically just a title. And this could be any model in your Laravel apps. OK, so over on the roots here, again, just super simple stuff. We've got an article index.
00:24
We're grabbing all of these articles, which will eventually order by the view count. And we've got a view being returned that shows the article.
00:33
And just so we know, over on the index, this is all we're doing. So pretty basic stuff. OK, so over on the article model itself, this is where we're going to create out a helper method
00:44
to go ahead and log a view. So let's go ahead and fill this in. And we'll use Redis to push this in. And then we'll get it back again.
00:51
OK, so how do we log a view? Well, let's go over to our web roots. And under here, when we look at the article, we're going to take that article in.
01:00
And we're just going to call that logView method. So over here, we're going to go ahead and use Redis. So we'll use the Redis facade. And remember, we looked at the commands earlier.
01:10
We've got pfAdd. That just maps up to our Redis instance. Now, in here, we need something like articles. And then the article ID.
01:19
So we could just easily switch this out with an sprintf. Or we could concatenate it. It doesn't really matter. And say articles.
01:28
And then have a placeholder. So in here, we could say, use that as a string. And say views, for example, because we might be storing other data.
01:35
And let's go ahead and pass in the article ID. And then as we saw from the last episode, we just need to pass some sort of unique thing in. So for this, I'm going to go ahead and just use the request
01:45
IP address. But of course, if you wanted to be a bit more thorough, you could pass that in or have some other unique key that you use.
01:52
It doesn't really matter as long as this is unique. OK, so now that we've done this, let's head over and just click on one of these articles. OK, so we know that our IP address is now
02:01
being pushed to that set with HyperLogLog. Now let's introduce a method to go ahead and grab the view count out. So let's say getViewCount.
02:10
And we already saw this earlier. We're going to go ahead and return using Redis again. And we're going to say pfCount. And we're going to use exactly the same key.
02:20
So let's just grab this directly from here. OK, let's go ahead and output the view count to make sure that we have it. So let's go over to the articles index.
02:28
And I'm just going to add it at the end of here. And we'll just say article getViewCount. OK, let's go back over to our index, give this a refresh. And yeah, sure enough, we've logged a view.
02:39
And once again, because we're passing the same IP address through, when I click on this again, it's not going to increment them views. Let's go ahead and click on another one and go back.
02:48
And yeah, sure enough, we're now logging unique views here. So what we could also do here if we head over to our web routes is just fake our IP address just to make sure that this is working.
02:58
So to do this within Laravel, we're going to grab our request object. We're going to access these server properties. And we're going to add to them.
03:05
And we're going to overwrite the remote address within PHP. So let's go ahead and do that. And let's set this to something completely different. So we'll say 127.0.0.2.
03:17
OK, if we head over now and give that a refresh, that is now our faked IP. And when I click on this, go back, there we go. We have got two views.
03:24
So really, really simple to integrate this directly into a model. A little bit later, we're going to look at how we can take this and add it to a trait.
03:32
So you can do it with any of your models very, very easily and have this functionality shared between them. But for now, that's pretty much it, logging views.
03:40
Now, what we want to talk about is how we would order these by the most viewed articles. At the moment, we can't do that. It just so happens that the top one has two views.
03:49
But we want to order these. And that involves syncing this data from Redis periodically back into our database. Let's go ahead and cover that next.
4 episodes 22 mins

Overview

If you need to log unique views in Laravel, you might reach for a database table to track IP addresses or another unique piece of data.

Let's take a look at speeding things up both in performance and complexity by using Redis and the HyperLogLog probabilistic data structure.

Once we're done, we'll set up a period command to sync views back to the database for easy ordering, and then create a trait to share functionality between other models.

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

Comments

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