PHPackages                             nh/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. nh/sortable

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

nh/sortable
===========

Make a Laravel model sortable.

1.3.0(3y ago)02261MITPHP

Since Apr 23Pushed 3y ago1 watchersCompare

[ Source](https://github.com/NatachaH/laravel-sortable)[ Packagist](https://packagist.org/packages/nh/sortable)[ RSS](/packages/nh-sortable/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (10)DependenciesVersions (13)Used By (1)

Installation
============

[](#installation)

Install the package via composer:

```
composer require nh/sortable

```

To make a model with position, you can create a migration via the console commande:

```
php artisan sortable:new {model? : the name of the model}

```

Then, add the **Sortable** trait to your model: *The default sortable values are position by asc. The trait will add the position column to your $fillable array*

```
use Nh\Sortable\Traits\Sortable;

use Sortable;

/**
 * Default sortable field and direction.
 * @var array
 */
protected $sortable = [
  'field' => 'position',
  'direction' => 'asc'
];

```

In option you can fire an event on the 'parent' model. Exemple if you sort the Media model in a Page model

```
/**
 * Default sortable field and direction.
 * @var array
 */
protected $sortable = [
  'field' => 'position',
  'direction' => 'asc',
  'event-on-parent' => true,
  'parent' => 'mediable'
];

```

In option you can also update the timestamps or not (updated\_at) By default it set to true

```
/**
 * Default sortable field and direction.
 * @var array
 */
protected $sortable = [
  'field' => 'position',
  'direction' => 'asc',
  'timestamp' => true
];

```

Javascript &amp; View
=====================

[](#javascript--view)

Add in your **package.json** the dependency:

```
"sortablejs": "^1.10.2",

```

And add in your JS file the Sortable script:

```
import Sortable from  '../../vendor/nh/sortable/resources/js/sortable';

```

Initialize the Sortable in your JS file.

```
var el = document.querySelector('#mySortable');
new Sortable(el, {
  successCallback: function(response){
      console.log('Position updated !');
  },
  errorCallback: function(response){
      console.log('You got an error');
  }
});

```

Then in your view, add the element:

- On the parent, add the attribute **data-sortable-model**
- On the parent, add the attribute **data-sortable-order** if you need to specify the order to rearanged
- On the children, add the attribute **data-id**
- On the children, add an element with class **.drag**

```

   One
   Two
   Three

```

Get collection by position
==========================

[](#get-collection-by-position)

You can get your model collection by the position: *You can pass the direction asc or desc*

```
$model->byPosition()
$model->byPosition('desc')

```

Get collection by sortable
==========================

[](#get-collection-by-sortable)

You can get your model collection by the sortable customer or default: *The values of your $sortable array will be take as default*

```
$model->sortable()
$model->sortable('name','desc')

```

Events
======

[](#events)

You can use the **SortableEvent** for dispatch events that happen where sorted happend. *You can pass a name, the model, and the number of model affected*

```
SortableEvent::dispatch('my-event', $model, $relation, 1);

```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

Established project with proven stability

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

Recently: every ~215 days

Total

12

Last Release

1214d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/47361520c40f781d7d5b39e26b3194800a35e89c4819c220ea3eaaaa907c34ec?d=identicon)[NatachaH](/maintainers/NatachaH)

---

Top Contributors

[![NatachaH](https://avatars.githubusercontent.com/u/24412486?v=4)](https://github.com/NatachaH "NatachaH (21 commits)")

### Embed Badge

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

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

PHPackages © 2026

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