Laravel counters

Laravel counters package

https://github.com/sertxudeveloper/laravel-counters

Codecov Test coverage

Manage counters in your Laravel application.

This package allows you to manage counters in your Laravel application. You can create a counter and increment or decrement it as you need. You can also create a counter with a series or a year, so you can have different counters for different series or years.

Requirements

This package requires Laravel 10.0 or higher.

Installation

You can install the package via composer:

composer require sertxudeveloper/laravel-counters

Usage

This package provides a configuration file, in order to publish it, you should run the following command:

php artisan vendor:publish --tag=counters-config

In this configuration file you will be able to configure the database connection the package should use for its migrations and models. By default, will use the default database connection defined in your Laravel project.

Next, you should run the migrations:

php artisan migrate

That's it! You are ready to use the package.

Here is an example of how to use the package:

Use a basic counter

use SertxuDeveloper\Counters\Counter;

$counter = Counter::make('INVOICES');

$invoice->number = $counter->next(); // 1

Use a counter with a series

use SertxuDeveloper\Counters\Counter;

$counter = Counter::make('INVOICES', series: 'N');

$invoice->number = $counter->next(); // 1
$invoice->number = $counter->next(); // 2

$counter = Counter::make('INVOICES', series: 'R');

$invoice->number = $counter->next(); // 1

Use a counter with a year

use SertxuDeveloper\Counters\Counter;

$counter = Counter::make('INVOICES', year: '2023');

$invoice->number = $counter->next(); // 1
$invoice->number = $counter->next(); // 2

$counter = Counter::make('INVOICES', year: '2024');

$invoice->number = $counter->next(); // 1

Use a counter with a year and a series

use SertxuDeveloper\Counters\Counter;

$counter = Counter::make('INVOICES', year: '2023', series: 'N');

$invoice->number = $counter->next(); // 1
$invoice->number = $counter->next(); // 2

$counter = Counter::make('INVOICES', year: '2023', series: 'R');

$invoice->number = $counter->next(); // 1

$counter = Counter::make('INVOICES', year: '2024', series: 'N');

$invoice->number = $counter->next(); // 1

Decrement a counter

use SertxuDeveloper\Counters\Counter;

$counter = Counter::make('INVOICES');

$invoice->number = $counter->next(); // 1
$invoice->number = $counter->next(); // 2

$invoice->number = $counter->decrement(); // 1

Custom increment

use SertxuDeveloper\Counters\Counter;

$counter = Counter::make('INVOICES');

$invoice->number = $counter->next(); // 1
$invoice->number = $counter->increment(5); // 6

Custom decrement

use SertxuDeveloper\Counters\Counter;

$counter = Counter::make('INVOICES');

$invoice->number = $counter->next(); // 1
$invoice->number = $counter->increment(5); // 6

$invoice->number = $counter->decrement(3); // 3