Home / Tutorials / How to implement visitor views / visitor counter in Laravel application
How to implement visitor views / visitor counter in Laravel application cover

How to implement visitor views / visitor counter in Laravel application

9 mins

13.8K

3 years ago

0 comments

Skilled

In this post, you'll be learning how to "implement a visitor counter" for any of your modal (e.g page or an article) that you have on your Laravel application. We'll be using the Eloquent Viewable package and the steps are very simple to follow, so get ready, and let's start implementing.

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"

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
}

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 count when user kept on refreshing the page. 
views($article)
    ->cooldown($minutes = 3)
    ->record();

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 this tutorial is helpful for you and if there's any question, do comment out below and let's start the discussion. Also, don't forget to share it with your friend as well and I would appreciate it a lot. Cheers and have a good try.
notion avatar

Ahmad Aziz

Week-end developer currently experimenting with web, mobile, and all things programming.

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Related Tutorials

Code Snippets

Collection of Laravel, AlpineJS, VueJS, Tailwind CSS, Flutter snippets and more. New snippets added every day.
View Snippets

Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your web projects
View Components

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this