maclervil's avatar

Counting page views

Please, what is the best way to implement Counting page views on my blog post in laravel?

1 like
6 replies
guybrush_threepwood's avatar
Level 33

Add a migration: php artisan make:migration add_reads_to_posts_table --table=posts

Edit the migration and add a "reads" column:

Schema::table('posts', function (Blueprint $table) {
    $table->bigInteger('reads')->unsigned()->default(0)->index();
});

And have a method on your Post model that increments the read count, for example:

public function incrementReadCount() {
    $this->reads++;
    return $this->save();
}

Then call the function in your PostController's show method before returning the view:

    public function show(Post $post)
    {
        $post->incrementReadCount();

        return view('post.show', compact($post));
    }
7 likes
RamjithAp's avatar

For your information. If you increment read count on the controller without any check then read count may not be accurate because people can refresh and increase it. So add cookie after visitor read then check for cookie in future. By this, you can have more accurate unique visits.

    public function show(Post $post)
    {
        if(Cookie::get($post->id)!=''){
		Cookie::set('$post->id', '1', 60);
		$post->incrementReadCount();
	}
        return view('post.show', compact($post));
    }

11 likes
antaugustol's avatar

@RamjithAp Got it working like this:

public function show(Post $post, Request $request )
    {
        if(! Auth::check()){//guest user identified by ip
        $cookie_name = (Str::replace('.','',($request->ip())).'-'. $post->id);
        } else {
            $cookie_name = (Auth::user()->id.'-'. $post->id);//logged in user
        }
        if(Cookie::get($cookie_name) == ''){//check if cookie is set
            $cookie = cookie($cookie_name, '1', 60);//set the cookie
            $post->incrementReadCount();//count the view
            return response()
            ->view('posts.show',[
                'post' => $post])
            ->withCookie($cookie);//store the cookie
        } else {
            return  view('posts.show',[
                'post' => $post]);//this view is not counted
        }
    }
3 likes
guybrush_threepwood's avatar

Fair enough. I tried to explain the easiest way to accomplish it since no specific requirements were given.

Without going so far, this forum's thread count works in a similar fashion. Just refresh the page and watch the read counter go up. But sure, for a blog post you might want to have a more accurate count.

4 likes
clinvest's avatar

You can also use in your controller

$post->increment('reads');

1 like
woodgas's avatar

I guess the appropriate way is to make a middleware with only string:

$request->post->increment('views_count');

The view counting is not a vote and it is not nesessary protect it against multiple reloading. By the way, don't use cookie to store info about post viewing. The size and amount of records are limited. In the end server will refused to set cookies and generate 502 bad gateway.

Please or to participate in this conversation.