outl1ne / nova-sortable by Tarpsvo

This Laravel Nova package allows you to reorder models in a Nova resource's index view using drag & drop.
1,602,864
273
6
Package Data
Maintainer Username: Tarpsvo
Package Create Date: 2019-10-31
Package Last Update: 2024-03-28
Language: Vue
License: MIT
Last Refreshed: 2024-04-18 15:27:34
Package Statistics
Total Downloads: 1,602,864
Monthly Downloads: 23,934
Daily Downloads: 1,143
Total Stars: 273
Total Watchers: 6
Total Forks: 118
Total Open Issues: 23

Nova Sortable

Latest Version on Packagist Total Downloads

This Laravel Nova package allows you to reorder models in a Nova resource's index view using drag & drop.

Uses Spatie's eloquent-sortable under the hood.

Requirements

  • Laravel Nova >= 2.10.0

Features

  • Drag & drop reorder (on either Index view or HasMany view)
  • BelongsTo/MorphsTo reorder support w/ pivot tables
  • Move to start and end arrows (makes item first/last)
  • Everything from eloquent-sortable
  • Localization

Screenshots

Sortable

Installation

Install the package in a Laravel Nova project via Composer:

# Install package
composer require optimistdigital/nova-sortable

Usage

Create migration

Add an order field to the model using Laravel migrations:

// Add order column to the model
Schema::table('some_model', function (Blueprint $table) {
  $table->integer('sort_order');
});

// Set default sort order (just copy ID to sort order)
DB::statement('UPDATE some_model SET sort_order = id');

Implement eloquent-sortable

Implement the Spatie's eloquent-sortable interface and apply the trait:

use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class SomeModel extends Eloquent implements Sortable
{
  use SortableTrait;

  public $sortable = [
    'order_column_name' => 'sort_order',
    'sort_when_creating' => true,
  ];

  ...
}

Apply HasSortableRows to Nova resource

Apply HasSortableRows trait from this package on the Resource:

use OptimistDigital\NovaSortable\Traits\HasSortableRows;

class MyResource extends Resource
{
  use HasSortableRows;

  ...
}

NB! This overrides the indexQuery() method.

Sorting on HasMany relationship

NB! The resource can only be sorted on either the Index view or the HasMany list view, but not both!

Sorting on HasMany is simple. Add 'sort_on_has_many' => true to the $sortable array on the model. Like so:

public $sortable = [
  'order_column_name' => 'sort_order',
  'sort_when_creating' => true,
  'sort_on_has_many' => true,
];

Sorting on ManyToMany relationships

Sorting on BelongsToMany and MorphToMany relationships is available, but requires special steps.

See the documentation here: Sorting ManyToMany relationships (w/ pivot table).

Localization

The translation file(s) can be published by using the following publish command:

php artisan vendor:publish --provider="OptimistDigital\NovaSortable\ToolServiceProvider" --tag="translations"

You can add your translations to resources/lang/vendor/nova-sortable/ by creating a new translations file with the locale name (ie et.json) and copying the JSON from the existing en.json.

Credits

License

Nova Sortable is open-sourced software licensed under the MIT license.