PHPackages                             thadbryson/eloquent-sequence - 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. thadbryson/eloquent-sequence

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

thadbryson/eloquent-sequence
============================

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

3.6.0(6y ago)011MITPHPPHP &gt;=7.1.3

Since Apr 20Pushed 5y agoCompare

[ Source](https://github.com/thadbryson/eloquent-sequence)[ Packagist](https://packagist.org/packages/thadbryson/eloquent-sequence)[ RSS](/packages/thadbryson-eloquent-sequence/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (40)Used By (0)

Eloquent Sequence
=================

[](#eloquent-sequence)

[![CircleCI](https://camo.githubusercontent.com/2f935f357277d7580801c813b7169d3c590824492e0d5f9813f15ef3304b24cf/68747470733a2f2f636972636c6563692e636f6d2f67682f68696768736f6c7574696f6e732f656c6f7175656e742d73657175656e63652e7376673f7374796c653d737667)](https://camo.githubusercontent.com/2f935f357277d7580801c813b7169d3c590824492e0d5f9813f15ef3304b24cf/68747470733a2f2f636972636c6563692e636f6d2f67682f68696768736f6c7574696f6e732f656c6f7175656e742d73657175656e63652e7376673f7374796c653d737667) [![StyleCI](https://camo.githubusercontent.com/08d98bd0e86352466ebf810da254eb775894911d4cfd384009a60d675dc6eec8/68747470733a2f2f7374796c6563692e696f2f7265706f732f36313534373931302f736869656c643f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/08d98bd0e86352466ebf810da254eb775894911d4cfd384009a60d675dc6eec8/68747470733a2f2f7374796c6563692e696f2f7265706f732f36313534373931302f736869656c643f6272616e63683d6d6173746572) [![License: MIT](https://camo.githubusercontent.com/c2bffd81d308ced1cc3b0d66fb0ed453ab478a5e17c988b780f9de986a390ee2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://opensource.org/licenses/MIT)

Easy creation and management sequence support for Eloquent models with elastic configuration.

[![Eloquent-Sequence by HighSolutions](https://raw.githubusercontent.com/highsolutions/eloquent-sequence/master/intro.jpg)](https://raw.githubusercontent.com/highsolutions/eloquent-sequence/master/intro.jpg)

Installation
------------

[](#installation)

This package can be installed through Composer:

```
composer require highsolutions/eloquent-sequence
```

Or by adding the following line to the `require` section of your Laravel webapp's `composer.json` file:

```
    "require": {
        "HighSolutions/eloquent-sequence": "3.*"
    }
```

Run `composer update` to install the package.

Version Compatibility
---------------------

[](#version-compatibility)

LaravelEloquent-Sequence5.1.x2.1.x5.2.x2.2.x5.3.x2.3.x5.4.x2.4.x5.5.x2.5.x5.6.x2.6.x5.x.x3.3.x6.x.x3.4.xUpdating Eloquent models
------------------------

[](#updating-eloquent-models)

```
use HighSolutions\EloquentSequence\Sequence;

class Section extends Model {

    use Sequence;

    public function sequence()
    {
        return [
            'group' => 'article_id',
            'fieldName' => 'seq',
        ];
    }
}
```

**Note:** as a field name do not use name of any exisiting method in that class, including `sequence`, as this will not work.

Configuration
-------------

[](#configuration)

You can specify four parameters:

- `group` - field name or field names (then input as an array) which narrows list of objects within sequence parameter will be calculated
    - default value: ""
- `fieldName` - field name in model to store sequence attribute
    - default value: "seq"
- `exceptions` - set this true if you want to catch exceptions during up/down methods
    - default value: false
- `orderFrom1` - set this true if your list starts from 1 not 0 / used for move method
    - default value: false
- `notUpdateOnDelete` - set this true if you don't want to update sequence attributes when delete an object
    - default value: false
- `disableTimestamps` - set this to true if you don't want to update the "updated\_at" attribute when using sequence methods
    - default value: false

Usage
-----

[](#usage)

Set sequence attribute
----------------------

[](#set-sequence-attribute)

Sequence attribute will be set during model creation.

```
$section = Section::create([
    'article_id' => 1,
    'title' => 'Lorem ipsum',
]);
```

After this metod field values of `$section` will be looking as:

```
{
    'id' => 1,
    'article_id' => 1,
    'title' => 'Lorem ipsum',
    'seq' => 1
}
```

When we create another Section objects:

```
Section::create([
    'article_id' => 1,
    'title' => 'Lorem ipsum Second',
]);
Section::create([
    'article_id' => 2,
    'title' => 'Lorem ipsum Third but new',
]);
```

We get list of objects with fields:

```
[{
    'id' => 1,
    'article_id' => 1,
    'title' => 'Lorem ipsum',
    'seq' => 1
}, {
    'id' => 2,
    'article_id' => 1,
    'title' => 'Lorem ipsum Second',
    'seq' => 2
}, {
    'id' => 3,
    'article_id' => 2,
    'title' => 'Lorem ipsum Third but new',
    'seq' => 1
}]
```

Delete object and update sequence
---------------------------------

[](#delete-object-and-update-sequence)

But when we delete object:

```
Section::find(1)->delete();
```

Sequence values will be updated accordingly:

```
[{
    'id' => 2,
    'article_id' => 1,
    'title' => 'Lorem ipsum Second',
    'seq' => 1
}, {
    'id' => 3,
    'article_id' => 2,
    'title' => 'Lorem ipsum Third but new',
    'seq' => 1
}]
```

Get objects with proper sequence
--------------------------------

[](#get-objects-with-proper-sequence)

To get object just add `->orderBy('seq', 'asc')` method:

```
Section::where('article_id', 1)->orderBy('seq', 'asc')->get();
```

or with local scope `sequenced`:

```
Section::where('article_id', 1)->sequenced()->get();
Section::where('article_id', 1)->sequenced('desc')->get();
```

Move object one position higher
-------------------------------

[](#move-object-one-position-higher)

To move object one position higher (swap position with earlier object) you only need to:

```
Section::find(2)->up();
```

This will set sequence attribute to one position lower and object one position lower will have sequence attribute changed to one position further.

Narrowing groups from configuration will be of course used.

Move object one position lower
------------------------------

[](#move-object-one-position-lower)

The same you can do it to make your object next in line:

```
Section::find(2)->down();
```

This will set Section ID=2 with sequence attribute like next Section object (based on sequence attribute) and swap their values accordingly.

Move object to the first position
---------------------------------

[](#move-object-to-the-first-position)

To move object to the first position, you only need to:

```
Section::find(2)->moveToFirst();
```

This will set sequence attribute to the first position in the sequence and will reorder the objects between the original position and the first position accordingly.

Narrowing groups from configuration will be of course used.

Move object to the last position
--------------------------------

[](#move-object-to-the-last-position)

To move object to the last position, you only need to:

```
Section::find(2)->moveToLast();
```

This will set sequence attribute to the last position in the sequence and will reorder the objects between the original position and the last position accordingly.

Narrowing groups from configuration will be of course used.

Move object to any position
---------------------------

[](#move-object-to-any-position)

You are able to move object to another position also. This is very useful when you are implementing drag&amp;drop functionality.

```
Section::find(2)->move(5);
```

This will set Section ID=2 with sequence attribute to 5th and rest objects' sequence attribute will be updated to match proper order.

Refresh positions in model
--------------------------

[](#refresh-positions-in-model)

Sometimes you may need to recalculate all position for given model (e.g. because of manually manipulating dataset). You can do it easily via:

```
Section::refreshSequence();
```

This static method will recalculate sequence attributes for every record for this model. Narrowing groups will be used as well as current sequence attribute of every record.

Check if object is first in the collection
------------------------------------------

[](#check-if-object-is-first-in-the-collection)

You are able to check if object is first in its group.

```
Section::find(2)->isFirst();
```

This will return true or false regarding is this a first element in the collection.

Check if object is not first in the collection
----------------------------------------------

[](#check-if-object-is-not-first-in-the-collection)

You are able to check if object is not first in its group.

```
Section::find(2)->isNotFirst();
```

This will return true or false regarding is this not a first element in the collection.

Check if object is last in the collection
-----------------------------------------

[](#check-if-object-is-last-in-the-collection)

You are able to check if object is last in its group.

```
Section::find(2)->isLast();
```

This will return true or false regarding is this a last element in the collection.

Check if object is not last in the collection
---------------------------------------------

[](#check-if-object-is-not-last-in-the-collection)

You are able to check if object is not last in its group.

```
Section::find(2)->isNotLast();
```

This will return true or false regarding is this not a last element in the collection.

Testing
-------

[](#testing)

Run the tests with:

```
vendor/bin/phpunit
```

Changelog
---------

[](#changelog)

3.6.0

- Add `isFirst`, `isNotFirst`, `isLast`, `isNotLast` method

3.5.0

- Fix to 3.3.0 relase - improved preventing timepstamp during resequencing

3.4.0

- Support Laravel 6.0 version

3.3.0

- Option to prevent update `updated_at` timestamp during resequencing

3.2.0

- Fix `moveToLast` method

3.1.0

- Possiblity to change group of exisitng object \[#16\]

3.0.2

- Little performance improvements

3.0.0

- Support all Laravel 5.x versions

2.6.2

- Add `NotUpdateOnDelete` configuration parameter

2.6.0

- Laravel 5.6 support

2.5.0

- Laravel 5.5 support

2.4.0

- Laravel 5.4 support

2.3.0

- Laravel 5.3 support

2.2.0

- Laravel 5.2 support

2.1.0

- Laravel 5.1 support

2.0.3

- add StyleCI and CircleCI support

2.0.2

- add methods `moveToFirst` and `moveToLast`

2.0.1

- add `InvalidArgumentException` when position argument is lower than first possible sequence value for `move` method

2.0.0

- full unit tests
- add `InvalidArgumentException` when position argument is bigger than last possible sequecne value for `move` method
- method `move` with invalid position argument (with exceptions parameter disabled) sets sequence attribute to first possible number (count + 1), instead of setting value from argument / BREAKING CHANGE

1.3.1

- fix `down` method when used on object with seq &gt; 2 (bug introduced in 1.3.0)

1.3.0

- fix `up` method when used on object with seq &gt; 2

1.2.0

- recalculation of sequence attribute on demand

1.1.1

- config for starting index of list (for move method)

1.1.0

- move objects to any position

1.0.0

- Add methods up and down

0.9.0

- Create package
- Create trait for automatic sequence attribute handling

Credits
-------

[](#credits)

This package is developed by [HighSolutions](https://highsolutions.org), software house from Poland in love in Laravel.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 65.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~30 days

Recently: every ~21 days

Total

31

Last Release

2408d ago

Major Versions

1.3.0 → 2.0.12018-01-02

1.3.1 → 2.0.22018-01-03

2.6.2 → 3.0.02018-09-20

2.6.3 → 3.0.12018-09-21

2.6.4 → 3.0.22018-11-26

PHP version history (4 changes)1.0.0PHP &gt;=5.6.4

2.2.0PHP &gt;=5.5.9

2.5.0PHP &gt;=7.0.0

2.6.0PHP &gt;=7.1.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/222212?v=4)[Thad Bryson](/maintainers/thadbryson)[@thadbryson](https://github.com/thadbryson)

---

Top Contributors

[![adammatysiak](https://avatars.githubusercontent.com/u/25105267?v=4)](https://github.com/adammatysiak "adammatysiak (25 commits)")[![tbryson-dialmycalls](https://avatars.githubusercontent.com/u/220066505?v=4)](https://github.com/tbryson-dialmycalls "tbryson-dialmycalls (4 commits)")[![Julien1138](https://avatars.githubusercontent.com/u/9657235?v=4)](https://github.com/Julien1138 "Julien1138 (3 commits)")[![vaughany](https://avatars.githubusercontent.com/u/381767?v=4)](https://github.com/vaughany "vaughany (3 commits)")[![luceos](https://avatars.githubusercontent.com/u/504687?v=4)](https://github.com/luceos "luceos (1 commits)")[![gpressutto5](https://avatars.githubusercontent.com/u/12385501?v=4)](https://github.com/gpressutto5 "gpressutto5 (1 commits)")[![Mombuyish](https://avatars.githubusercontent.com/u/8007787?v=4)](https://github.com/Mombuyish "Mombuyish (1 commits)")

---

Tags

laravelmodeleloquentsequenceorder

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/thadbryson-eloquent-sequence/health.svg)

```
[![Health](https://phpackages.com/badges/thadbryson-eloquent-sequence/health.svg)](https://phpackages.com/packages/thadbryson-eloquent-sequence)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[gurgentil/laravel-eloquent-sequencer

A package that allows you to create and manage sequences on Eloquent models

15246.7k](/packages/gurgentil-laravel-eloquent-sequencer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
