PHPackages                             alvits/laravel-eloquent-position - 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. alvits/laravel-eloquent-position

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

alvits/laravel-eloquent-position
================================

Position logic for Eloquent models with minimum setup

v1.0.5(7y ago)014MITPHP

Since Jan 25Pushed 7y ago1 watchersCompare

[ Source](https://github.com/alvits77/laravel-eloquent-position)[ Packagist](https://packagist.org/packages/alvits/laravel-eloquent-position)[ RSS](/packages/alvits-laravel-eloquent-position/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (7)Used By (0)

Laravel eloquent model position
===============================

[](#laravel-eloquent-model-position)

Position logic for Eloquent models with minimum setup. Before saving it will check if the position has changed and updates the other entries based on the models position value.

[![Total Downloads](https://camo.githubusercontent.com/cb7b2fb02dd3ba0a3a4a38da38496a0c494f15e7cf3ea731adad56c29d0c8af5/68747470733a2f2f706f7365722e707567782e6f72672f616c766974732f6c61726176656c2d656c6f7175656e742d706f736974696f6e2f646f776e6c6f6164733f666f726d61743d666c6174)](https://packagist.org/packages/alvits/laravel-eloquent-position)[![Latest Stable Version](https://camo.githubusercontent.com/5f4ff6341201e0eb6e68c299a137d013e6f9ac9357257c679e4a48a30a220c8c/68747470733a2f2f706f7365722e707567782e6f72672f616c766974732f6c61726176656c2d656c6f7175656e742d706f736974696f6e2f762f737461626c653f666f726d61743d666c6174)](https://packagist.org/packages/alvits/laravel-eloquent-position)[![Latest Unstable Version](https://camo.githubusercontent.com/5b00094d874f6527505d67878fa53f810a9f42310f0bca9f445c2d72f9a7ad77/68747470733a2f2f706f7365722e707567782e6f72672f616c766974732f6c61726176656c2d656c6f7175656e742d706f736974696f6e2f762f756e737461626c653f666f726d61743d666c6174)](https://packagist.org/packages/alvits/laravel-eloquent-position)

- [Installation](#installation)
- [Usage](#usage)
    - [Migration example](#migration-example)
    - [Events](#events)
        - [Positioning](#positioning)
        - [Positioned](#positioned)
    - [Command](#command)
    - [Traits](#traits)
- [Changelog](#changelog)
- [Todo](#todo)
- [Contribution](#contribution)

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

[](#installation)

**Install via composer**

```
composer require alvits/laravel-eloquent-position

```

Usage
-----

[](#usage)

1. Add a `position` (can be custom) column in your table (model)
2. Add `PositionTrait` into your model (if you are using custom column set the `$positionColumn` property)
3. If you are using grouped entries (like parent\_id and etc), you can set the `$positionGroup` with the column name/names (supports single string or multiple columns)
4. Add to form the position input (can be input\[type=number\] and etc) and fill/set the position on save
5. When position is null or empty string, the last position will be used.
6. If you are not using migration (the column exists), run the php artisian model:position` command to fix current entries (it will create correct order)

**Then you can get your entries sorted:**

```
// ASC
YourModel::sorted()->get()

// DESC
YourModel::sortedByDESC()->get()
```

If using default column name (position), the value will be converted to numeric value (if not null or empty string).

**Get the position**Use the `$model->getPosition()` or use the standard way by using the column name `$model->position`

### Migration example

[](#migration-example)

```
public function up()
    {
    Schema::table('pages', function (Blueprint $table) {
        $table->smallInteger('position')->default(0)->after('id');
    });

    // Update the order pages
    Artisan::call('model:position', [
        'model'=> \App\Models\Page\Page::class
    ]);
}
```

### Model example

[](#model-example)

```
class Page extends Model
{
    use PositionTrait;

    public $table = 'pages';
    public $positionGroup = ['parent_slug'];

    protected $fillable = [
        'title', 'slug', 'parent_slug', 'content', 'description', 'position'
    ];

}
```

### Events

[](#events)

You can listen to events for positioning changes. You can use the `PositionEventsTrait` for easy model registration.

```
....

class YourModel extends Model {
    use PositionTrait, PositionEventsTrait;
    ....
}
```

#### Positioning

[](#positioning)

Called before running the last position calculation and the final movement of other entries for given position.

**Enables to:**

- Restore the position to original value - return false
- Add additional query conditions via AbstractPositionQuery object in second parameter ($query-&gt;query() =&gt; Builder)

Name: `positioning`

```
YourModel::positioning(function($model, $query) {
    $query->query()->where('type', 'type'); // or etc
    \Log::info('positioning', 'To '.$model->getPosition().' from '.$query->oldPosition());
});
```

#### Positioned

[](#positioned)

Name: `positioned`

Example via trait:

```
YourModel::positioned(function($model) {
    /// TODO
});
```

### Command

[](#command)

#### Reposition command

[](#reposition-command)

This command will help you to fix the order of your models. You must provide a model class. You must include the `RecalculatePositionCommand` into your Console `Kernel` class.

```
php artisan model:position App\\Models\\YourModel
```

### Traits

[](#traits)

#### PositionTrait

[](#positiontrait)

Uses the `BasePositionTrait` and `PositionScopeTrait`

You can set:

- *string* `positionColumn` *to enable overriding for the position column*
- *boolean* `disablePositionUpdate` *disables the updated of other entries*
- *string|array* `positionGroup` *builds a filter from columns for position calculation. Supports single column or multiple columns*
- *string* `defaultPositionValue` *allows returning different value when position is empty string or null. Default value is null*

#### PositionScopeTrait

[](#positionscopetrait)

Todo
----

[](#todo)

- Add the custom position trait to enable automatic convert to numeric value (don't want to use the setAttribute method) - In progress
- Add service provider for automatic command registration
- Add all docs for all features
- Add next/prev scope functions in `PositionScopeTrait`
- Add `PositionHelperTrait` with (getLastUsedPosition, getNextPosition($position = null))

Contribution
------------

[](#contribution)

See [CONTRIBUTING.md](CONTRIBUTING.md) for how to contribute changes. All contributions are welcome.

Copyright and License
---------------------

[](#copyright-and-license)

[laravel-eloquent-position](https://github.com/alvits77/laravel-eloquent-position)was written by [Martin Kluska](http://kluska.cz) and is released under the [MIT License](LICENSE.md).

Copyright (c) 2016 Martin Kluska

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 83.3% 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 ~142 days

Recently: every ~151 days

Total

6

Last Release

2731d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8134366?v=4)[Alexandr Tselishchev](/maintainers/alvits77)[@alvits77](https://github.com/alvits77)

---

Top Contributors

[![pionl](https://avatars.githubusercontent.com/u/1878831?v=4)](https://github.com/pionl "pionl (5 commits)")[![mariodro](https://avatars.githubusercontent.com/u/10945762?v=4)](https://github.com/mariodro "mariodro (1 commits)")

###  Code Quality

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/alvits-laravel-eloquent-position/health.svg)

```
[![Health](https://phpackages.com/badges/alvits-laravel-eloquent-position/health.svg)](https://phpackages.com/packages/alvits-laravel-eloquent-position)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11222.5M33](/packages/anourvalar-eloquent-serialize)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135212.4k7](/packages/statamic-rad-pack-runway)[mozex/laravel-scout-bulk-actions

Import, flush, and queue-import all your Laravel Scout searchable models at once. Auto-discovers models, runs in bulk, tracks progress.

1437.7k](/packages/mozex-laravel-scout-bulk-actions)[ramadan/easy-model

A Laravel package for enjoyably managing database queries.

111.6k](/packages/ramadan-easy-model)

PHPackages © 2026

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