PHPackages                             maher/laravel-counters - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. maher/laravel-counters

ActivePackage[Utility &amp; Helpers](/categories/utility)

maher/laravel-counters
======================

management for counters in laravel system

v1.0.4(11mo ago)4511.2k↓28.1%4[1 issues](https://github.com/MaherSaleem/laravel-counters/issues)MITPHPPHP &gt;=7.0

Since Jul 15Pushed 11mo ago3 watchersCompare

[ Source](https://github.com/MaherSaleem/laravel-counters)[ Packagist](https://packagist.org/packages/maher/laravel-counters)[ RSS](/packages/maher-laravel-counters/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)DependenciesVersions (6)Used By (0)

Counters Management for laravel project.
========================================

[](#counters-management-for-laravel-project)

- [Installation](#installation)
- [Usage](#usage)
    - [1) Using Counters Associated with model](#1-using-counters-with-no-models)
    - [2) Using Counters with no models](#2-using-counters-with-no-models)
    - [3) Using artisan commands](#3-using-artisan-commands)
    - [4) Using APIs for counters](#4-usign-apis-for-counters)
- [Database Seeding](#database-seeding)
- [Cache\[To be added later\]](#cache)
- [Credits](#credits)
- [Special Thanks](#special-thanks)
- [license](#license)

In some cases, you need to manage the state of the counters in your laravel project, like the number of visitors of your website, or number of view for a post, or number of downloads for a file, this needs to create a new table to save these records, or at least adding new column for your tables to save the count value.
Therefore, with this package, you can create as many counters for your model without needing to create a physical column in your model's table. Moreover, you can store public counters like "number\_of\_visitors" for your website, without needing to create a whole table to store it.

In summary, this package allows you to manage counters in your laravel project.

Once installed you can do stuff like this:

```
//increment/decrement system counters
Counters::increment('number_of_visitors');

// increment/decrement model objects counters
$post->incrementCounter('number_of_views');
$feature->decrementCounter('number_of_bugs');
$user->decrementCounter('balance', 2); // decrement the balance of the user by 2
```

There are many other methods that are mentioned below.

Installation
------------

[](#installation)

- [Laravel](#laravel)

### Laravel

[](#laravel)

This package can be used in Laravel 5.4 or higher. If you are using an older version of Laravel You can install the package via composer:

```
composer require maher/laravel-counters
```

In Laravel 5.5 and higher versions, the service provider will automatically get registered. In older versions of the framework just add the service provider in `config/app.php` file:

```
'providers' => [
    // ...
    Maher\Counters\CountersServiceProvider::class,
    //...
];
```

You must publish [the migration](https://github.com/MaherSaleem/Counters/tree/master/database/migrations) with:

```
php artisan vendor:publish --provider="Maher\Counters\CountersServiceProvider" --tag="migrations"
```

After the migration has been published you can create the tables by running the migrations:

```
php artisan migrate
```

You can publish the [config](https://github.com/MaherSaleem/Counters/tree/master/config) file with:

```
php artisan vendor:publish --provider="Maher\Counters\CountersServiceProvider" --tag="config"
```

Usage
-----

[](#usage)

### 1) Using Counters with no models

[](#1-using-counters-with-no-models)

First, add the `Maher\Counters\Traits\HasCounter` trait to your model(s): for example we can add it to Post Model

```
use Maher\Counters\Traits\HasCounter;

class Post extends Model
{
    use HasCounter;

    // ...
}
```

This package allows the posts to be associated with counters. Every post can associated with multiple counters.

We can create a counter like this, for example, lets create a counter for the number of views for the Post Model.

```
use Maher\Counters\Models\Counter;

$counter = Counter([
            'key' => 'number_of_views',
            'name' => 'Views',
            'initial_value' => 0 //(could be left empty, default value is 0)
            'step' => 1 // (could be left empty, default value is 1)
        ]);
```

After that, for example, in the show function of post controller, you can add this line:

```
class PostsController extends Controller
{
    public function show(Post $post)
    {
        //...
        $post->incrementCounter('number_of_views');
        //...
    }
}
```

By doing this, the counter of number\_of\_views for every post will be incremented \[by the step size\] as we show the post.

This package has another functions.

```
// will return the counter object
$post->getCounter($key);

// will return the counter value
$post->getCounterValue($key);

//will add record in countrable table for this post object
$post->addCounter($key);

//will remove the record from countrable table for this post object.
$post->removeCounter($key);

//increment the counter with the given $key
//Note that this will create record in countrable table,if it's not exist
//if $step is entered, it will increment with the value of $step
$post->incrementCounter($key, $step = null);

//decrement the counter with the given $key
//Note that this will create record in countrable table,if it's not exist
//if $step is entered, it will decrement with the value of $step
$post->decrementCounter($key, $step = null);

 // will reset the counter value (to initial_value) for the post object.
$post->resetCounter($key);
```

### 2) Using Counters with no models.

[](#2-using-counters-with-no-models)

Sometimes, you have general counters that are not associated with any models, for example the number visitor for your website.

Therefore, this package will allow you to deal with Counter with these types.

```
use Maher\Counters\Facades\Counters;
class Test
{
    public function incrementFunction()
    {
        //moreover you can add this function in your public page to be incremented
        //every time user hits your website
        Counters::increment('number_of_visitors');
    }
}
```

This Facade has many other functions:

```
// will return the counter object
Counters::get($key);

// will return the counter value
Counters::getValue($key, $default = null);

// set the value of the counter
Counters::setValue($key, $value);

//set the step of the counter
Counters::setStep($key, $step);

//increment the counter with the given $key
Counters::increment($key, $step = null);

//decrement the counter with the given $key
Counters::decrement($key, $step = null);

 // will reset the counter for the inital_value
Counters::reset($key);
```

In some cases, you want to increment the counter once for every person, for example no need to increment the number\_of\_visitors counter every time the same user refreshes the page.

So you can use these functions:

```
Counters::incrementIfNotHasCookies($key);
Counters::decrementIfNotHasCookies($key);
```

3)Using artisan commands
------------------------

[](#3using-artisan-commands)

You can create a Counter from a console with artisan commands. The following command creates the counter number\_of\_visitors with initial value 0 and step 1

```
php artisan make:counter number_of_visitors Visitors 0 1
```

4) Using APIs for counters
--------------------------

[](#4-using-apis-for-counters)

In some cases, we are using a single page application, or we don't want to leave the current page. Therefore there are APIs to increment/decrement general/per\_model\_object counters.

Examples

```
use Maher\Counters\Facades\Counters;

//this will return a link to increment the counter key
//for exampel 'exmple.com/counters/increment/visitors'
Counters::getIncrementUrl($key);
Counters::getDecrementUrl($key);

// we can use these in Blades for example,
// Incrment Visitors
```

by Using this link, a json structure of the updated counter will return as response

```
{
    "counter": {
        "id": 1,
        "key": "visitors",
        "name": "Visitors",
        "initial_value": 0,
        "value": 9,
        "step": 3,
        "notes": null,
        "created_at": "2018-07-02 20:57:03",
        "updated_at": "2018-07-07 13:07:49"
    }
}
```

Moreover, we can increment/decrement objects counters by these methods

```
$post = Post::find(1);

$post->getIncrementUrl($key);
$post->getDecrementUrl($key);
```

And a json structure like the following will return as response

```
{
    "counterable": {
        "id": 2,
        "counterable_id": 2,
        "counterable_type": "App\\Post",
        "counter_id": 1,
        "value": 24,
        "created_at": null,
        "updated_at": "2018-07-07 13:09:41",
        "counter": {
            "id": 1,
            "key": "visitors",
            "name": "Visitors",
            "initial_value": 0,
            "value": 9,
            "step": 3,
            "notes": null,
            "created_at": "2018-07-02 20:57:03",
            "updated_at": "2018-07-07 13:07:49"
        }
    }
}
```

Database Seeding
----------------

[](#database-seeding)

Here's a sample seede.

```
    use Illuminate\Database\Seeder;
    use Maher\Counters\Facades\Counters;

    class CounterTableSeeder extends Seeder
    {
        public function run()
        {

            // create Counters
            //This will create a counter with inital value as 3, and every increment 5 will be added.
            Counter::create([
                'key' => 'number_of_visitors',
                'name' => 'Visitors',
                'initial_value' => 3,
                'step' => 5
            ]);
            //This counter will has 0 as inital_value and 1 as step
            Counter::create([
                'key' => 'number_of_visitors2',
                'name' => 'Visitors2'
            ]);

            $viewCounter = Counter::create([
                'key' => 'number_of_views',
                'name' => 'Views'
            ]);

            $post = Post::find(1);
            $post->addCounter('number_of_views');// to add the record to countrable table

        }
    }
```

Credits
-------

[](#credits)

Maher Khdeir
[Linked In](https://www.linkedin.com/in/maher-khdeir/)
Email:

Special Thanks
--------------

[](#special-thanks)

Special Thanks to [Spatie](https://github.com/spatie) , since I learned and followed there structure of building laravel packages.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance51

Moderate activity, may be stable

Popularity37

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 97.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~629 days

Total

5

Last Release

347d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1e6db9479ff532e76a0dc56ab099e117b9f6621a0777b6766abab9e3a0d96eed?d=identicon)[maher\_saleem](/maintainers/maher_saleem)

---

Top Contributors

[![MaherSaleem](https://avatars.githubusercontent.com/u/14930756?v=4)](https://github.com/MaherSaleem "MaherSaleem (33 commits)")[![hewerthomn](https://avatars.githubusercontent.com/u/226773?v=4)](https://github.com/hewerthomn "hewerthomn (1 commits)")

---

Tags

counterdecrementincrementlaravelphpviewsvisitorslaravelvisitorsincrementcountersDecrementmahernumber of visitors

### Embed Badge

![Health badge](/badges/maher-laravel-counters/health.svg)

```
[![Health](https://phpackages.com/badges/maher-laravel-counters/health.svg)](https://phpackages.com/packages/maher-laravel-counters)
```

###  Alternatives

[stephenjude/filament-blog

Filament Blog Builder

20317.8k](/packages/stephenjude-filament-blog)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)[datomatic/nova-detached-actions

A Laravel Nova tool to allow for placing actions in the Nova toolbar detached from the checkbox selection mechanism.

11229.2k](/packages/datomatic-nova-detached-actions)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
