Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Nova Sortable

optimistdigital/nova-sortable image

Nova Sortable stats

Downloads
1.4M
Stars
261
Open Issues
11
Forks
109

View on GitHub →

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

Nova Sortable

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

  • php: >=8.0
  • laravel/nova: ^4.24.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

Installation

Install the package in a Laravel Nova project via Composer:

# Install package
composer require outl1ne/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,
];
 
...
}

When the model does not have a sortable configuration, the default eloquent-sortable configuration will be used.

Apply HasSortableRows to Nova resource

Apply HasSortableRows trait from this package on the Resource:

use Outl1ne\NovaSortable\Traits\HasSortableRows;
 
class MyResource extends Resource
{
use HasSortableRows;
 
...
}

NB! This overrides the indexQuery() method.

Disallowing sorting on a per-request/resource basis

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! This requires you to disable caching (see below).

Disabling caching

If you want to disable caching due to using canSort or running tests, you can set sortableCacheEnabled to false on the resource that has the HasSortableRows trait. See the example below:

class Artist extends Resource
{
use HasSortableRows;
 
public static $sortableCacheEnabled = false;
}

Or if you want to temporarily disable sortability cache (for tests), you can call Resource::disableSortabilityCache() on the resource.

Custom sortable options

Nova sorting order

To sort your resource in a different order in Nova, you can set the nova_order_by flag to DESC (ASC by default) in the $sortable array.

class SomeModel extends Eloquent implements Sortable
{
use SortableTrait;
 
public $sortable = [
'order_column_name' => 'sort_order',
'sort_when_creating' => true,
'nova_order_by' => 'DESC',
];
 
...
}

Ignoring policies

If you have a resource that has authorizedToUpdate false, but you want the user to still be able to sort it, you can use the ignore_policies flag like so:

class SomeModel extends Eloquent implements Sortable
{
use SortableTrait;
 
public $sortable = [
'order_column_name' => 'sort_order',
'sort_when_creating' => true,
'ignore_policies' => true,
];
 
...
}

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,
];

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 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="Outl1ne\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.

Other usecases

Using indexQuery

This package overwrites the indexQuery of the Resource and if you still want to use it, you can do it as follows:

use HasSortableRows {
indexQuery as indexSortableQuery;
}
 
public static function indexQuery(NovaRequest $request, $query)
{
// Do whatever with the query
// ie $query->withCount(['children', 'descendants', 'modules']);
return parent::indexQuery($request, static::indexSortableQuery($request, $query));
}

Credits

License

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

outl1ne photo

Previously we were Optimist Digital. Now we're @outl1ne. If you wish to use our packages, please consider switching to the outl1ne namespace.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Optimistdigital Nova Sortable Related Articles

Laravel Nova is Now Available image

Laravel Nova is Now Available

Read article
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel