This Laravel Nova package allows you to reorder models in a Nova resource's index view using drag & drop.
cierrateam/nova-sortable is a Laravel package for this laravel nova package allows you to reorder models in a nova resource's index view using drag & drop..
It currently has 0 GitHub stars and 27 downloads on Packagist (latest version 2.1.7).
Install it with composer require cierrateam/nova-sortable.
Discover more Laravel packages by cierrateam
or browse all Laravel packages to compare alternatives.
Last updated
This package is forked from optimistdigital/nova-sortable. The difference is that the edit buttons are on the left side instead of the right side.
php: >=7.3laravel/nova: ^3.0Install the package in a Laravel Nova project via Composer:
# Install package
composer require cierrateam/nova-sortable
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 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,
];
...
}
When the model does not have a sortable configuration, the default eloquent-sortable configuration will be used.
Apply HasSortableRows trait from this package on the Resource:
use Cierrateam\NovaSortable\Traits\HasSortableRows;
class MyResource extends Resource
{
use HasSortableRows;
...
}
NB! This overrides the indexQuery() method.
You can disable sorting on a per-request or per-resource basis by overriding the canSort() on the Resource method like so:
public static function canSort(NovaRequest $request, $resource)
{
// Do whatever here, ie:
// return user()->isAdmin();
// return $resource->id !== 5;
return true;
}
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,
];
The sort on has many configuration can be apply in a per model basis or it can be added in the eloquent-sortable configuration for all the models.
return [
// Spatie sortable configuration
/**
* Add sort on has many in all the models.
**/
'sort_on_has_many' => true,
];
Sorting on BelongsToMany and MorphToMany relationships is available, but requires special steps.
See the documentation here: Sorting ManyToMany relationships (w/ pivot table).
The translation file(s) can be published by using the following publish command:
php artisan vendor:publish --provider="Cierrateam\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.
Nova Sortable is open-sourced software licensed under the MIT license.