We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Vien Hoang • 11 years ago

Hi,
Thanks for a great tutorial. I had a hard time to add an fire Event when I began this tutorial, couldn't figure out what was wrong in the beginning.
Not sure if this line of code is incorrect or not. In the documentaction subscribe only take one argument.
Event::subscribe('posts.view', 'Stidges\Events\ViewPostHandler');
I changed it to and it works now
Event::listen('posts.view', 'Stidges\Events\ViewPostHandler');

Thanks again, will be looking forward to more useful tutorials :)

Tom Schlick • 11 years ago

Eloquent has an increment() method that would be better to use than doing it yourself manually. The reason for this is that using eloquent's method does the current+1 in SQL instead of fetching the result and then saving it again. You may run into some race conditions if two requests come in at the same time and are not aware of each other.

Stidges • 11 years ago

Yeah that's true. The reason I don't use it here is because the result is already needed. Also the increment method unfortunately does not increment the value on the model itself, so you would have to do it manually again anyway

xiaozi • 11 years ago

`+= 1` is not safe when someone visit the page at the same time.

Stidges • 11 years ago

Okay, thanks for your responses! I have fixed it in the post :)

Abba Bryant • 11 years ago

$post->view_counter = $post->increment('view_counter');

Stidges • 11 years ago

That would set the view counter to the number of affected rows, which is always 1

rohit naidu • 11 years ago

Why do we need to check the expired timestamp on every page load? Can't the same be accomplished by checking only when that post is viewed again?
If I view the post again and timestamp hasn't expired update the timestamp.

meetgodhani • 11 years ago

Interesting Article. But I don't know if you have noticed or not. Using the above code for the pageview and pasting the specific page link to social media especially Twitter. Your page view drastically increases right away on paste. For example just paste ( Only paste and compose tweet. Do not click ) this link in your twitter http://www.laravel-tricks.c... . And notice that your views would increase right away without click by 10. Anyone know how to overcome or solve that problem ? It is going through multiple Twitter server requests.

Paritosh Maity • 7 years ago

Sir which link u provided is good ...but how to implement view count as that site implemented ...show code ..pls!!!!!!!!

Oludare Odunfa G • 11 years ago

Really nice. Lesson learnt

Romain 'Maz' BILLOIR • 6 years ago

Thanks for this post.

But I think the event way is not perfect or may be upgraded:
The event is triggered anytime the post is requested in a collection. So anytime it's rendered in a list of post (like in index, or even in backend index, or in a sidebar which display "latest posts" maybe...), the post view fields will be increment, even if the reader doesn't look at it.

I prefer use the increment() method in PostController@show or the event way with a filter on request to increment only if the route is the guest post show one.

Bit manger • 7 years ago

Could you show me demo of this tutorial! :)

Hamza Dastgir • 8 years ago

Guyz i need urgent help .. start/global.php is not in laravel 5 what to do ???
a newbie here

Stidges • 8 years ago

This tutorial is only compatible with Laravel 4.x. I will add an updated post this week for a Laravel 5 compatible version!

Cyril de Wit • 8 years ago

Since I needed this functionality for a lot of Laravel applications, I created a package that everyone can install with composer. It contains this functionality but also with some other great features. It's under active development. I also accept feature requests at the issue tracker.
https://github.com/cyrildew...

Note: I don't think my package is compatible for old Laravel versions.

I also saw that Frank Cieslik created a package but that didn't fit my needs.

Shane Daugherty • 8 years ago

Might be a good idea to do the increment using a queue to not slow down the response

saugat thapa • 8 years ago

https://github.com/Kryptoni... is used for laravel 5.

David • 8 years ago

how to dowload?????

devdojo.com • 9 years ago

Nice Right up. Thanks for the thorough article :)

Teranode • 10 years ago

can you do one for Laravel 5?

Frank Cieslik • 10 years ago

Look at my package - it's now compatible with the 5th version.

Frank Cieslik • 10 years ago

I build a package for that kind of like and view functionality with some additional features. Maybe interesting for somebody: https://github.com/fraank/V...

fajar Lewelwng • 6 years ago

are it work for 5.8

Wasif Iqbal • 8 years ago

Hey frank,

I was trying to integrate your package into one of my laravel's project but I am getting this when i composer require fraank/view-counter

Could not find package fraank/view-counter at any version for your minimum-stability (stable). Check the package spelling or your minimum-stability

Can you please check on this and let me know what I am doing wrong ?

Thanks in advance.

Jan Iwanow • 10 years ago

Many thanks for the article it helped me a lot!

thuandh • 11 years ago

Thanks for share