Surfe.be - passive income

How to Implement Post View Counter in Laravel

Post click counter

Today we are going to know how to implement Post/Page views count using Laravel . We can say it is going to be click counter or single post visitor counter . We will create this traffic counter using Laravel. If a user visits a page or post then our Database field name view_count is automatically incremented . Hope you enjoy this . 

traffice counter in laravel

Post click counter

When a user can click a single post link and will go to single post page then we will just increment our database field view_count. See below code -

class PostController extends Controller
{
    public function post(post $post)
    {          
           $Key = 'blog' . $post->id;
             if (\Session::has($Key)) {

             \DB::table('posts')
                ->where('id', $post->id)
                ->increment('view_count', 1);
              \Session::put($Key, 1);
            }
            

          // Write your code which you want 
      }
}

That’s it. Hope you will like it. if you face any error please leave a comments & don’t forget to share.

Leave a comments

LET'S SOCIALITE

Recent Tweets