Grid Sortable
super-admin-org/grid-sortable adds drag-and-drop ordering to a grid. Drag rows by the handle, then click Save order; the new positions are written to the model's order column. It uses SortableJS in the browser and spatie/eloquent-sortable on the model.
| Package | super-admin-org/grid-sortable |
| Adds | $grid->sortable() |
| Route | POST /admin/_grid-sortable_ |
Installation
composer require super-admin-org/grid-sortable
No admin:import is needed; there is no menu item.
Usage
Make the model sortable:
use Illuminate\Database\Eloquent\Model;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
class Slide extends Model implements Sortable
{
use SortableTrait;
public $sortable = [
'order_column_name' => 'order_column',
'sort_when_creating' => true,
];
}
The table needs the column, e.g. $table->unsignedInteger('order_column')->default(0)->index();.
Enable it in the grid:
protected function grid(): Grid
{
$grid = new Grid(new Slide());
$grid->sortable();
$grid->column('title');
return $grid;
}
A handle column appears first and the grid is ordered by the order column (unless the user sorts by another column). After a row is dragged, a Save order button appears in the grid tools.
Ordering works within the current page. To reorder across pages, raise the page size first.
Translation
The button text is Save order. Translate it in lang/<locale>.json:
{ "Save order": "অর্ডার সংরক্ষণ" }
Permissions
Save order posts to /admin/_grid-sortable_. Administrators can always do this. For other roles, add a permission with the HTTP path /_grid-sortable_ (method POST) and assign it to the role, or they get "permission denied".
Compatibility
| Laravel | PHP | eloquent-sortable | State |
|---|---|---|---|
| 8 | 8.0+ | 3.x | Expected to work |
| 9 | 8.0+ | 4.x | Expected to work |
| 10 – 13 | 8.2+ | 4.x or 5.x | Expected to work |
The package does not pin an eloquent-sortable version yet; Composer picks the newest one your app allows. To choose, require it yourself, e.g. composer require spatie/eloquent-sortable:^4.0.