Home / Snippets / Laravel Visitors Counter
Laravel Visitors Counter cover

Laravel Visitors Counter

3.7K

2 years ago

0 comments

To implement Laravel visitor counter you can make use of a package called awssat/laravel-visits and it's very simple to implement.

1 - Install Dependency and Publish Configs
Install the dependency and then publish the necessary configuration.
composer require awssat/laravel-visits

Publish the configuration as follows.
php artisan vendor:publish --provider="Awssat\Visits\VisitsServiceProvider" --tag=config

The migration is important to use the "eloquent" engine.
php artisan vendor:publish --provider="Awssat\Visits\VisitsServiceProvider" --tag=migrations

Run the migration to migrate the "visits" table.
php artisan migrate

2 - Update Config
Update the config to use "eloquent" as the engine which otherwise it will be set to default. The benefit of using eloquent is that later on, you will be able to reference the relationship of the associated model.
# config/visits.php

'engine' => 'eloquent'

3 - Integrate Views with The Model
Now you can initialize the views to the model that you want the visitors to be counted to. Imagine you have an "Article" model and you want to count the total of user's visits, you can define it like below.
# app/Models/Article.php

class Article extends Model
{
    /**
     * Get the instance of the user visits
     *
     * @return \Awssat\Visits\Visits
     */
    public function visitsCounter()
    {
        return visits($this);
    }

    /**
     * Get the visits relation
     * 
     * @return \Illuminate\Database\Eloquent\Relations\Relation
     */
    public function visits()
    {
        return visits($this)->relation();
    }
}

4 - Increment the Views
Now that you have integrated the model with the views, you can increment each time a user visits the page by calling the "increment()" method.
# app/Http/Controllers/ArticleController.php

php artisan show(Article $article)
{
    $article->visitsCounter()->increment();

    return view('article.show', ['article' => $article]);
}

Now every time that "show" method is called the number of visitors will be incremented.

5 - Retrieve total Visitors
To get the total number of visitors and the associated data you can make use of the following methods
$article = Article::first();

visits($article)->period('day')->count();
visits($article)->period('week')->count();
visits($article)->period('month')->count();
visits($post)->countries();
visits($post)->refs();
visits($post)->operatingSystems();
visits($post)->languages();

If you are looking for more available methods, you can directly refer to the documentation for more.
notion avatar

Ahmad Aziz

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

Topics:

Frontend

Resource

Average

Average

Support Us

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

Post Tutorials

Learn trending tutorials for ReactJs, React Native, VueJS, NextJs, Laravel, Tailwind CSS, and Flutter development.
View Tutorials

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