PHPackages                             mtsanford/laravel-arrangeable - 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. mtsanford/laravel-arrangeable

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

mtsanford/laravel-arrangeable
=============================

Arrangeable behaviour for Laravel eloquent models

v0.3.2(7y ago)1101MITPHPPHP &gt;=7.0

Since Sep 2Pushed 7y ago1 watchersCompare

[ Source](https://github.com/mtsanford/laravel-arrangeable)[ Packagist](https://packagist.org/packages/mtsanford/laravel-arrangeable)[ RSS](/packages/mtsanford-laravel-arrangeable/feed)WikiDiscussions master Synced yesterday

READMEChangelog (5)Dependencies (4)Versions (6)Used By (0)

Arrangeable behavior for Laravel models
=======================================

[](#arrangeable-behavior-for-laravel-models)

This package provides a trait that adds an arrangeable behavior to an Laravel model. Instances of arrangeable models have an order column with values 0,1,2... Arrangeable models by default do not use a foreign key for grouping, but a foreign key can be specified in configuration, where each group will have it's own ordering. Compatible with Lavavel ^5.5.

Instalation
-----------

[](#instalation)

You can install using composer:

```
composer require mtsanford/laravel-arrangeable

```

Usage
-----

[](#usage)

To add arrangeable behavior to your model, use the trait `MTSanford\LaravelArrangeable\ArrangeableTrait`, optionally specifying a foreign key in configuration to group models.

```
use MTSanford\LaravelArrangeable\ArrangeableTrait;

class MyModel extends Model
{

    use ArrangeableTrait;

    public $arrangeableConfig = [
        'foreign_key' => 'foreign_id',
    ];

}
```

### Methods

[](#methods)

#### ArrangeableTrait::arrangeableMove(array, int | null)

[](#arrangeabletraitarrangeablemovearray-int--null)

This will move a list of models specified by id to a target group defined by a foreign key, appending them to the end of the target group in the order specified. Models from the target group can be in the source list. Groups with removed items will have their orders adjusted appropriately.

```
// Move models 4, 5, and 2 to the end of the group with foreign key = 1
MyModel::arrangeableMove([4,5,2],1);

// | id | foreign_id | order |                | id | foreign_id | order |
// | -- | ---------- | ----- |                | -- | ---------- | ----- |
// | 1  | 1          | 0     |    BECOMES     | 1  | 1          | 0     |
// | 2  | 1          | 1     |   =========>   | 2  | 1          | 4     |
// | 3  | 1          | 2     |                | 3  | 1          | 1     |
// | 4  | 2          | 0     |                | 4  | 1          | 2     |
// | 5  | 2          | 1     |                | 5  | 1          | 3     |
// | 6  | 2          | 2     |                | 6  | 2          | 0     |
```

If all the models are in the same foreign key group and will remain there, there is no need to specify it. Reordering an entire group is just a special case of this.

```
// reverse the order of the models with foreign key = 2
MyModel::arrangeableMove([6,5,4]);

// | id | foreign_id | order |                | id | foreign_id | order |
// | -- | ---------- | ----- |                | -- | ---------- | ----- |
// | 1  | 1          | 0     |    BECOMES     | 1  | 1          | 0     |
// | 2  | 1          | 1     |   =========>   | 2  | 1          | 1     |
// | 3  | 1          | 2     |                | 3  | 1          | 2     |
// | 4  | 2          | 0     |                | 4  | 2          | 2     |
// | 5  | 2          | 1     |                | 5  | 2          | 1     |
// | 6  | 2          | 2     |                | 6  | 2          | 0     |
```

Also if the model has no foreign key, there is no need to specify it.

```
// Move models 2 and 1 to the end of the arrangement group
MyModel::arrangeableMove([2,1]);

// | id | order |                | id | order |
// | -- | ----- |                | -- | ----- |
// | 1  | 0     |    BECOMES     | 1  | 3     |
// | 2  | 1     |   =========>   | 2  | 2     |
// | 3  | 2     |                | 3  | 0     |
// | 4  | 3     |                | 4  | 1     |
```

#### ArrangeableTrait::arrangeableFixOrder(int | null)

[](#arrangeabletraitarrangeablefixorderint--null)

A convenient utility should your operations cause the ordering to become irregular. Again, the foreign key parameter is only needed if there is a foreign key specified in $arrangeableConfig (see below).

```
MyModel::arrangeableFixOrder(1);

// | id | foreign_id | order |                | id | foreign_id | order |
// | -- | ---------- | ----- |                | -- | ---------- | ----- |
// | 1  | 1          | 0     |    BECOMES     | 1  | 1          | 0     |
// | 2  | 1          | 5     |   =========>   | 2  | 1          | 1     |
// | 3  | 1          | 8     |                | 3  | 1          | 2     |
// | 4  | 2          | 0     |                | 4  | 2          | 0     |
```

### Create and Delete

[](#create-and-delete)

By default, 'creating' and 'deleted' model events are listened to, and the orders within the group are kept up to date.

```
new MyModel(['foreign_id' => 1]);

// | id | foreign_id | order |                | id | foreign_id | order |
// | -- | ---------- | ----- |                | -- | ---------- | ----- |
// | 1  | 1          | 0     |    BECOMES     | 1  | 1          | 0     |
// | 2  | 1          | 1     |   =========>   | 2  | 1          | 1     |
// | 3  | 1          | 2     |                | 3  | 1          | 2     |
//                                            | 4  | 1          | 3     |

MyModel::find(2)->delete();

// | id | foreign_id | order |                | id | foreign_id | order |
// | -- | ---------- | ----- |                | -- | ---------- | ----- |
// | 1  | 1          | 0     |    BECOMES     | 1  | 1          | 0     |
// | 2  | 1          | 1     |   =========>   | 3  | 1          | 1     |
// | 3  | 1          | 2     |
```

### Arrange query scope

[](#arrange-query-scope)

```
MyModel::arrange()->get()->pluck('id')->all();

// | id | order |
// | -- | ----- |
// | 1  | 0     |
// | 2  | 1     |   =======>  [1,2,4,3]
// | 3  | 3     |
// | 4  | 2     |
```

### Configuration

[](#configuration)

These are the default configuration settings:

```
    protected static $arrangeableConfigDefaults = [
        'primary_key'    => 'id',
        'order_key'      => 'order',
        'foreign_key'    => NULL,
        'start_order'    => 0,
        'handle_create'  => true,
        'handle_delete'  => true,
    ];

//        primary_key:     primary key of the model
//        order_key:       the column in the model that holds the order
//        foreign_key:     order will be maintained with models that have same foreign key
//                         or in the entire table if NULL.
//        start_order      value of order_key for the start of the list
//        handle_create    automatically set order_key on new models to end of list?
//        handle_delete    automatically maintain order when a model is removed?

```

To override any of them, define a static property $arrangeableConfig in your model.

```
class MyModel extends Model
{
    use ArrangeableTrait;

    public static $arrangeableConfig = [
        'foreign_key'  => 'parent_id',
        'start_order'  => 1,
    ];
}
```

Tests
-----

[](#tests)

The package contains some phpunit tests, using Orchestra. After installation run:

```
$ vendor/bin/phpunit

```

Credits
-------

[](#credits)

Inspired by [spatie/eloquent-sortable](https://github.com/spatie/eloquent-sortable).

About
-----

[](#about)

Mark Sanford is a developer in San Francisco.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

5

Last Release

2808d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d16eea702ac00418582420581d022c2cf4603cc5c8075be64d9dffce120ff914?d=identicon)[mtsanford](/maintainers/mtsanford)

---

Top Contributors

[![mtsanford](https://avatars.githubusercontent.com/u/554707?v=4)](https://github.com/mtsanford "mtsanford (12 commits)")

---

Tags

laravelmodeleloquenttraitsortablesortbehaviourarrangearrangeable

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mtsanford-laravel-arrangeable/health.svg)

```
[![Health](https://phpackages.com/badges/mtsanford-laravel-arrangeable/health.svg)](https://phpackages.com/packages/mtsanford-laravel-arrangeable)
```

###  Alternatives

[spatie/eloquent-sortable

Sortable behaviour for eloquent models

1.5k22.9M268](/packages/spatie-eloquent-sortable)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[cybercog/laravel-ban

Laravel Ban simplify blocking and banning Eloquent models.

1.1k651.8k11](/packages/cybercog-laravel-ban)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[jedrzej/sortable

Sortable trait for Laravel's Eloquent models - sort your models using request parameters

54261.0k1](/packages/jedrzej-sortable)[jedrzej/pimpable

Laravel 4/5/6 package that allows to dynamically filter, sort and eager load relations for your models using request parameters

105179.0k1](/packages/jedrzej-pimpable)

PHPackages © 2026

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