Posts Learn Components Snippets Categories Tags Tools About
/

How to Sort Model in Laravel Nova (Ordering Priority)

Learn how to sort model in Laravel Nova to easily manage the ordering priority

2 years ago

8 mins read

3242 views

In this post, you'll learn how to sort models in Laravel Nova to easily manage the ordering priority. Since Laravel Nova doesn't come with the ability to sort models by default, we'll be using a package called optimistdigital/nova-sortable. The steps are very straight so let's get started.

Install Dependencies


Firstly, let's install the dependency with a composer package manager.
composer require optimistdigital/nova-sortable
Once it's ready, create a new migration command to add a new column to record the ordering of the model. For this example. let's assume we are going to order the "Post" model and the new column is "sort_order".
php artisan make:migration add_sort_order_to_posts_table --table=posts
Inside the "up" method, do add the code for the column definition then run "php artisan migrate" to migrate it.
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddSortOrderToPostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->integer('sort_order')->default(1);
        });

        // set default sort order (make use of the ID to sort order)
        \DB::statement('UPDATE posts SET sort_order = id');
    }
}

Nova Sort Model Implementation


Since by default this library makes use of the Spatie eloquent-sortable package, we have to add the interface and trait to the model we are going to be sorting. In this case, it's the "Post" model.
<?php

namespace App\Models;

use Spatie\EloquentSortable\Sortable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements Sortable
{
  use SortableTrait;

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

   # other codes ommited
}
For the nova resource file, define the HasSortableRows trait and with that, the model now should be sortable.
<?php

namespace App\Nova;

use OptimistDigital\NovaSortable\Traits\HasSortableRows;

class Post extends Resource
{
    use HasSortableRows;

    # other codes ommited
}

If you refresh the dashboard, now the sorting icon should appear on each and every row for the particular model. By dragging it up and down, the sort order will automatically be updated in the backend.
Laravel Nova Model before sorted

Laravel Nova Model after sorted

Has Many Relationship


If there's a scenario where you have to sort on a has many relationships, you will have to pass on the "sort_on_has_many" config and you can define it as follows in the model's declaration.
public $sortable = [
  'order_column_name' => 'sort_order',
  'sort_when_creating' => true,
  'sort_on_has_many' => true,
];
By now you should be able to sort models in Laravel Nova and easily manage the ordering priority of each resource. If you find it helpful do share it with your friends and happy coding. 🍻

Extra Reading

Alternative Tags

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)