enea / laravel-sequenceable
Facilitates the generation and autocompletion of a sequential value in the database
Installs: 8 381
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
- laravel/framework: ^10.21|^v11.15
- vaened/php-sequence-generator: ^2.0.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.4
README
Laravel Sequenceable is a library to generate and manage sequences for laravel models.
// simple sequence Serie::lineal('document_number'); // sequence with scope Serie::lineal('document_number')->scope('invoice'); // sequence with scope and fixed length Serie::lineal('document_number')->scope('invoice')->length(8);
Installation
Laravel Sequenceable requires PHP 8.1.
To get the latest version, simply require the project using Composer:
composer require enea/laravel-sequenceable
Once installed, if you are not using automatic package discovery, then you need to register the Enea\Sequenceable\SequenceableServiceProvider
service provider in your config/app.php
.
Now. Publish the configuration file.
php artisan vendor:publish --provider='Enea\Sequenceable\SequenceableServiceProvider'
And finally run migrations.
php artisan migrate
Basic Usage
Getting started with the library is as simple as using the Sequenceable
trait and implementing the SequenceableContract
interface, after that you only need to specify the sequences you want to generate.
<?php namespace App; use Enea\Sequenceable\Contracts\SequenceableContract; use Enea\Sequenceable\Sequenceable; use Enea\Sequenceable\Serie; use Illuminate\Database\Eloquent\Model; class Document extends Model implements SequenceableContract { use Sequenceable; public function sequencesSetup(): array { return [ Serie::lineal('document_number') ]; } }
Advanced
We exemplify all the options to generate a sequence
with the case of a payment document.
- To start, we need a column to store the sequence, and for this we will use a column called
number
.
public function sequencesSetup(): array { return [ Serie::lineal('document_number') ]; }
- Now that we have the column defined, we realize that we need to create a separate sequence for each type of document. For this problem, the library offers the possibility of adding an scope for the column.
public function sequencesSetup(): array { return [ Serie::lineal('document_number')->scope($this->type()) ]; } protected function type(): string { return $this->payment_document_type; }
- Everything is fine, but now we want the sequences not to be saved in a numeric value, but instead to be a text string with a fixed length of 10.
public function sequencesSetup(): array { return [ Serie::lineal('document_number')->scope($this->type())->length(10) ]; }
-
Concluding, we could also say that we do not want to use the default table for sequences and we need a special table to store the payment sequences, for this you have to create your own sequence table.
We can wrap a block of sequences using the class
Enea\Sequenceable\Wrap::create
public function sequencesSetup(): array { return [ Wrap::create(PaymentSequence::class, fn(Wrap $wrap) => $wrap->column('document_number')->scope($this->type())->length(10)) ]; }
List
To retrieve all the sequences of a model, you can use the Enea\Sequenceable\Facades\Succession
facade which is linked to the Enea\Sequenceable\Succession
class.
$collection = Succession::from(Document::class);
This returns an instance of Enea\Sequenceable\SequenceCollection
. With which you can do things like:
// return all sequences $collection->all(); // find sequence by name $collection->find('document_number', 'invoice');
Config
You can change the default sequence model of config\sequenceable.php
in the model
key.
<?php return [ /* |-------------------------------------------------------------------------- | Sequence Model |-------------------------------------------------------------------------- | | This key defines the base sequence model that will be used to generate the autoincrementable | values, you can modify this key and define your own sequence model whenever | you implement the SequenceContract interface or extend the base model */ 'model' => \Enea\Sequenceable\Model\Sequence::class, ];
Or explicitly specify the model you want to use with certain fields, you can achieve this from the configuration of the sequences in your model.
public function sequencesSetup(): array { return [ Wrap::create(CustomSequence::class, function(Wrap $wrap): void { $wrap->column('column-name'); $wrap->column('another-column-name'); //.. }), ]; }
Customize
if you already have a model to store your sequences, you need to implement the Enea\Sequenceable\Contracts\SequenceContract
interface, or extend the default model Enea\Sequenceable\Model\Sequence
.
In case you have your own sequence model, there are some fields that you should store in its sequence table:
- The column ID, and this is obtained by concatenating the column name and scope.
- The name of the table to which the sequence belongs.
- An integer type sequence.
Example
To better exemplify this, we will use the default Sequence
model.
This model comes with a default configuration.
Migration
The table structure has the required fields, you can see the migration in CreateSequencesTable
You can see another example of this in the test
folder and look for the files CustomSequence.php
and migrations/2017_04_23_200525_create_custom_sequences_table.php
More documentation
You can find a lot of comments within the source code as well as the tests located in the tests
directory.