PHPackages                             hippieua/laravel-sortablev2 - 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. hippieua/laravel-sortablev2

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

hippieua/laravel-sortablev2
===========================

Trait to sort order in Laravel model

v1.0.1(1y ago)014[1 issues](https://github.com/hippieua/laravel-sortablev2/issues)MITPHPPHP &gt;=8.0

Since Jun 12Pushed 1y ago1 watchersCompare

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

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

Laravel Sortable v2
===================

[](#laravel-sortable-v2)

[![Latest Stable Version](https://camo.githubusercontent.com/a5c76415fed8ce0f5ab46b8b7ace6be82f6df749278c50673120b0af0dd9844c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68697070696575612f6c61726176656c2d736f727461626c6576322e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hippieua/laravel-sortablev2)[![Total Downloads](https://camo.githubusercontent.com/24b2c44b45828c5696080bd994fc5580df90700a233d1540ee14a6d9cbb3c187/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68697070696575612f6c61726176656c2d736f727461626c6576322e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hippieua/laravel-sortablev2)[![License](https://camo.githubusercontent.com/445e43784c80d6b7b40eec88c78ab986b2958560c220d6fd37b3271c2f446e0b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f68697070696575612f6c61726176656c2d736f727461626c6576322e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hippieua/laravel-sortablev2)

Laravel Sortable v2 is a Laravel package designed to easily add sortable behavior to Eloquent models. This package allows you to manage the order of database records via simple trait inclusion.

Features
--------

[](#features)

- **Sortable Trait**: Include a trait in your Eloquent models to enable sortable functionality.
- **Automatic Order Management**: Automatically manages the order field during creation and provides methods to move records up or down.
- **Relation Support**: Handles sorting within the context of a parent relationship, ideal for nested resources or grouped items.

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

[](#installation)

To install the package, run the following command in your Laravel project:

```
composer require hippieua/laravel-sortablev2
```

Usage
-----

[](#usage)

### Setup

[](#setup)

1. **Make migration to add sortable field to your model**

```
php artisan make:migration "Add order_id to Categories"
```

```
public function up(): void
{
    Schema::table('categories', function (Blueprint $table) {
        $table->unsignedBigInteger('order_id')->default(0)->after('id');
    });

    Category::orderBy('id')->each(fn($category, $index) => $category->update(['order_id' => $index++]));
}
```

2. **Include the Trait in Your Model**

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Hippieua\Sortable2\Sortable2;

class Chapter extends Model
{
    use Sortable2;
}
```

2. **Define Sortable Field and Optional Relation**

Override methods in your model if you need a custom sortable field or a specific relation for sorting:

```
protected function getSortableField(): string
{
    return 'custom_order_field';  // Default is 'order_id'
}

protected function getSortableRelation(): ?BelongsTo
{
    return $this->belongsTo(ParentModel::class);  // Default is null
}
```

### Managing Order

[](#managing-order)

- **Move an Item Up**

```
$chapter = Chapter::find(1);
$chapter->moveUp();
```

- **Move an Item Down**

```
$chapter = Chapter::find(1);
$chapter->moveDown();
```

Events
------

[](#events)

The package hooks into several Eloquent model events to ensure data integrity:

- `creating`
- `saving`
- `updating`
- `deleting`
- `retrieved`
- Conditionally `restoring` if SoftDeletes is used.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.0
- Laravel 9.x to 11.x

Contributing
------------

[](#contributing)

Contributions are welcome, and any issues or pull requests should be submitted on the [GitHub repository](https://github.com/hippieua/laravel-sortablev2).

License
-------

[](#license)

The Laravel Sortable v2 package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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 ~1 days

Total

2

Last Release

698d ago

### 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 (7 commits)")

---

Tags

laravellaravel-sortable-models

### Embed Badge

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

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

###  Alternatives

[silber/bouncer

Eloquent roles and abilities.

3.6k4.4M25](/packages/silber-bouncer)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8703.0M17](/packages/yajra-laravel-oci8)[cybercog/laravel-love

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

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)

PHPackages © 2026

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