turahe/laravel-counters

management for counters in laravel system

0.0.2 2021-10-16 10:23 UTC

This package is auto-updated.

Last update: 2024-04-03 22:30:40 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require Scrutinizer Code Quality PHP Composer

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_downloads" 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_downloads'); 

// 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

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 turahe/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' => [
    // ...
    Turahe\Counters\CountersServiceProvider::class,
    //...
];

You must publish the migration with:

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

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

php artisan migrate

Usage

1) Using Counters with no models

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

use Turahe\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 Turahe\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 counterable table for this post object
$post->addCounter($key);

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

//increment the counter with the given $key
//Note that this will create record in counterable 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 counterable 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.

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 Turahe\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_downloads');
    }
}

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 initial_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_downloads 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

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

php artisan make:counter number_of_downloads Visitors 0 1

Database Seeding

Here's a sample seeder.

    use Illuminate\Database\Seeder;
    use Turahe\Counters\Facades\Counters;

    class CounterTableSeeder extends Seeder
    {
        public function run()
        {

            // create Counters
            //This will create a counter with initial value as 3, and every increment 5 will be added.
            Counter::create([
                'key' => 'number_of_downloads',
                'name' => 'Visitors',
                'initial_value' => 3,
                'step' => 5
            ]);
            //This counter will have 0 as initial_value and 1 as step
            Counter::create([
                'key' => 'number_of_downloads2',
                '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 counterable table
            
            
        }
    }