Laravel

Laravel Unique Visitor Counter

In this article, we will learn about Laravel unique visitor counter, We’ll be using the Eloquent Viewable package and the steps are very simple to follow, so get ready, and let’s start implementing.

We will do the following things in this blog.

  1. Installation
  2. Implementation
  3. Recording Views
  4. Retrieving Views

Step-1 Installation

Require the package to your Laravel application using composer.

composer require cyrildewit/eloquent-viewable

This package by default will require us to “publish” the migration manually so let’s publish it and run the migration.

php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="migrations"
php artisan migrate

If you prefer to change the table name, do change the table name on the migration file and then run the migration command. Other than that, the configuration can also be published and you can change some of the predefined configs from the file if necessary.

php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="config"

Step-2 Implementation

To allow the package to record the total user visit, your model class will have to implement the “Viewable Interface”. Aside from that, make use of the “InteractsWithViews” trait to provide the functionality of recording and retrieving the views count.

use Illuminate\Database\Eloquent\Model;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use CyrildeWit\EloquentViewable\Contracts\Viewable;

class Article extends Model implements Viewable
{
    use InteractsWithViews;

    // others code ommited
}

Step-3 Recording Views

In order for you to record the views, you can call the “views” helper method and pass in the model that you have defined to be viewable and chain with the “record” method to increment the count value by one.

views($article)->record();

This code is typically written inside the controller method and it will look something like below. Do note that you can record multiple views by defining the code multiple times.

// ArticleController.php
public function show(Article $article)
{
    views($article)->record();

    return view('articles.show', compact('article'));
}

One of my recommendations is to also add a cooldown for each anonymous user visiting the page by calling the “cooldown” method and passing the total time in minutes. This is very important to prevent duplicate counts when the user kept on refreshing the page. 

views($article)
    ->cooldown($minutes = 3)
    ->record();

Step-4 Retrieving Views

Finally, to retrieve the total number of views for the model, just call the helper method and chain it with the “count” method as follows. This will retrieve the total article count and it’s accessible with the variable you have defined.

$totalViews = views($article)->count();

If you want to limit by 2 date range, do specify the period range like below and the result will be limited to the particular range.

use CyrildeWit\EloquentViewable\Support\Period;

// Example: get views count from 2020 upto 2021
views($article)
    ->period(Period::create('2020', '2021'))
    ->count();

That’s not all though, you can refer to the package documentation for more details on what are the available methods you can make use of.

I hope now you have the basic idea of how we can count the page view via eloquent, rate me 5 to keep me motivated.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button