Download the PHP package bnbwebexpertise/laravel-sequence without Composer
On this page you can find all versions of the php package bnbwebexpertise/laravel-sequence. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bnbwebexpertise/laravel-sequence
More information about bnbwebexpertise/laravel-sequence
Files in bnbwebexpertise/laravel-sequence
Package laravel-sequence
Short Description Laravel package to handle model sequence generation (no gap)
License MIT
Informations about the package laravel-sequence
Laravel 5.4+ integer sequence helpers
This package provides helpers to work with no-gap and ordered integer sequences in Laravel models.
It has been built to help the generation of "administrative sequences" where there should be no missing value between records (invoices, etc).
WARNING This is beta version (POC). This is not yet production ready, so you should use at your own risk.
Installation
Add the service provider to your configuration :
Configuration
You can customize this package behavior by publishing the configuration file :
php artisan vendor:publish --provider='Bnb\Laravel\Sequence\SequenceServiceProvider'
You can customize values without publishing by specifying those keys in your .env
file :
To avoid concurrency issues when generating sequence number, the queue worker number should be set to one. It is recommended to use a dedicated queue (and worker).
Adding a sequence to a model
Sequence columns should be generated with the following configuration in your migration :
$table->unsignedInteger('sequence_name')->nullable()->unique();
To work with sequence you must enhance your model class with the Bnb\Laravel\Sequence\HasSequence
trait.
The sequences
array property of your model must contain the list of the sequences names :
Some sequence properties can be overridden by specifying a method in the model class (where MySequence
is the name your sequence in PascalCase) :
- sequence start value (per-sequence) with the
getMySequenceStartValue() : int
- sequence format (per-sequence) with the
formatMySequenceSequence($next, $last) : int
- sequence generation authorization (per-sequence) with the
canGenerateMySequenceSequence() : bool
- sequence gap filling mode (per-sequence)
isMySequenceGapFilling() : bool
- sequence generation queue connection (for the model class)
getSequenceConnection() : string
- sequence generation queue name (for the model class)
getSequenceQueue() : string
- sequence auto-dispatch activation (for the model class)
isSequenceAutoDispatch() : bool
Example :
Sequence generation event
When a sequence number is generated a model event is thrown for running custom tasks.
You can listen to the sequence event using the sequenceGenerated($sequenceName, $callback)
method (in a service provider boot method for example) :
Schedule generation
You can schedule inside your console kernel the Bnb\Laravel\Sequence\Console\Commands\UpdateSequence
at the required frequency to generate the missing sequence numbers asynchronously.
This may not be required when using auto dispatch mode but can be used as a security fallback.