Count page views in laravel 7

Hello World, Today we will learn how to count page views in Laravel 7. In this tutorial, we simply count the page views whenever the user visits the page. Suppose we have a post detail page and we want to show how many times this post has been viewed by users. We will use shorthand Laravel query methods to solve this problem.


Post::find($post_id)->increment('views');

The above method will increase 1 every time. If you want to increase 2 or more then you can change the above code as follows.

Post::find($post_id)->increment('views',2);

 

Leave a Reply