PHPackages                             vaened/laravel-sequenceable - 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. [Database &amp; ORM](/categories/database)
4. /
5. vaened/laravel-sequenceable

ActiveLibrary[Database &amp; ORM](/categories/database)

vaened/laravel-sequenceable
===========================

Facilitates the generation and autocompletion of a sequential value in the database

v5.1.1(8mo ago)3109.5k—0%MITPHPPHP ^8.2CI passing

Since Jun 26Pushed 8mo ago1 watchersCompare

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

READMEChangelog (7)Dependencies (4)Versions (19)Used By (0)

Laravel Sequenceable Package
============================

[](#laravel-sequenceable-package)

[![Build Status](https://github.com/vaened/laravel-sequenceable/actions/workflows/test.yml/badge.svg)](https://github.com/vaened/laravel-sequenceable/actions?query=workflow%3ATests)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

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

[](#installation)

Laravel Sequenceable requires PHP 8.1.

To get the latest version, simply require the project using Composer:

```
composer require vaened/laravel-sequenceable
```

Now. Publish the configuration file.

```
php artisan vendor:publish --tag='sequenceable'
```

And finally run migrations.

```
php artisan migrate
```

Basic Usage
-----------

[](#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.

```
namespace App;

use Vaened\Sequenceable\Contracts\SequenceableContract;
use Vaened\Sequenceable\Sequenceable;
use Vaened\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
--------

[](#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](#customize).

    ```
    public function sequencesSetup(): array
    {
        return [
            Wrap::create(PaymentSequence::class,
                         fn(Wrap $wrap) => $wrap->column('document_number')->scope($this->type())->length(10))
        ];
    }
    ```

    > We can wrap a block of sequences using the class `Vaened\Sequenceable\Wrap::create`

List
----

[](#list)

To retrieve all the sequences of a model, you can use the `Vaened\Sequenceable\Facades\Succession` facade which is linked to the [`Vaened\Sequenceable\Succession`](https://github.com/vaened/laravel-sequenceable/blob/master/src/Succession.php) class.

```
$collection = Succession::from(Document::class);
```

This returns an instance of [`Vaened\Sequenceable\SequenceCollection`](https://github.com/vaened/laravel-sequenceable/blob/master/src/SequenceCollection.php). With which you can do things like:

```
// return all sequences
$collection->all();

// find sequence by name
$collection->find('document_number', 'invoice');
```

Config
------

[](#config)

You can change the default sequence model of `config\sequenceable.php` in the `model` key.

```
