PHPackages                             hippieua/laravel-sortable - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. hippieua/laravel-sortable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hippieua/laravel-sortable
=========================

Trait to sort order in Laravel model

1.0.1(4y ago)1135MITPHPPHP &gt;=7.3

Since Jun 26Pushed 2y ago1 watchersCompare

[ Source](https://github.com/hippieua/laravel-sortable)[ Packagist](https://packagist.org/packages/hippieua/laravel-sortable)[ Docs](https://github.com/hippieua/laravel-sortable)[ RSS](/packages/hippieua-laravel-sortable/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

Laravel Sortable
================

[](#laravel-sortable)

[![Latest Version on Packagist](https://camo.githubusercontent.com/0bd7750ad220ca4d76238e4c36f659d1688d9086acebfb285d33b0edfe70dd08/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68697070696575612f6c61726176656c2d736f727461626c65)](https://packagist.org/packages/hippieua/laravel-sortable)[![Total Downloads](https://camo.githubusercontent.com/f9527adb0894d905a067ea112422e943c03e5eec35d27faac33dfe2e6b310469/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68697070696575612f6c61726176656c2d736f727461626c65)](https://packagist.org/packages/hippieua/laravel-sortable)

This package allows you to sort order of your Laravel model records with easy methods:

```
$model->moveUp();
$model->moveDown();
$model->move('up');
$model->move('down');
```

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

[](#installation)

To install the most recent version, run the following command.

```
composer require hippieua/laravel-sortable
```

Setup
-----

[](#setup)

Your model should contain sortable field. You can add new or use existing one (don't use default id field)

```
Schema::table('comments', function (Blueprint $table) {
    $table->unsignedInteger('order')->default(1);
});
```

Update you model, use Sortable trait and fill `$sortable_field` property according to field name you've added.

```
    use Hippie\Sortable\Sortable;

    class Comment extends Model
    {
        use Sortable;

        protected string $sortable_field = 'order';

        protected $fillable = [
            'order',
        ];
    }
```

If your model have BelongsTo relationship, and you want to move records up and down within your relation items fill `$sortable_relation` property.

```
use Hippie\Sortable\Sortable;

class Comment extends Model
{
    use Sortable;

    protected string $sortable_field = 'order';
    protected string $sortable_relation = 'post';

    protected $fillable = [
        'order',
    ];

    public function post(): BelongsTo
    {
        return $this->belongsTo(Post::class);
    }
```

```
class Post extends Model
{
    public function comments(): BelongsTo
    {
        return $this->hasMany(Comment::class)->orderBy('order');
    }
}
```

In case you don't want to calculate your sortable field value on creating new item manually add `static::created` to your sortable model

```
protected static function booted()
{
    static::created(function ($model) {
        $model->updateSortOrderOnCreate();
    });
}
```

Usage
-----

[](#usage)

### Add new item

[](#add-new-item)

```
$comment = $post->comments()->create([
    'text' => $request->text,
])
```

`$comment->order` value will be set to `$post->comments->count() + 1`

### Sort existing items

[](#sort-existing-items)

```
$post->comments()->first()->moveDown()
```

will set `order` field value for the first comment to 2 and set `order` field value for second item to 1;

### Available methods

[](#available-methods)

```
$comment->moveUp();
$comment->moveDown();
$comment->move('up');
$comment->move('down');
```

Info
----

[](#info)

Tested on Laravel 8 and 9 versions

Credits
-------

[](#credits)

- [Vitalii Gura](https://github.com/hippieua)

License
-------

[](#license)

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

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

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

Total

2

Last Release

1462d ago

PHP version history (2 changes)1.0PHP &gt;=7.1

1.0.1PHP &gt;=7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/82d62f1cd1ba7c748b12a6003e843fba0c0652bdbe47497e3517c3895a53b0e8?d=identicon)[hippieua](/maintainers/hippieua)

---

Top Contributors

[![hippieua](https://avatars.githubusercontent.com/u/1492178?v=4)](https://github.com/hippieua "hippieua (8 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

---

Tags

laravellaravel-sortable-models

### Embed Badge

![Health badge](/badges/hippieua-laravel-sortable/health.svg)

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

###  Alternatives

[stephenjude/filament-blog

Filament Blog Builder

20619.4k](/packages/stephenjude-filament-blog)[datomatic/nova-detached-actions

A Laravel Nova tool to allow for placing actions in the Nova toolbar detached from the checkbox selection mechanism.

11273.0k](/packages/datomatic-nova-detached-actions)

PHPackages © 2026

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