novius/laravel-nova-order-nestedset-field

A Laravel Nova field that make your resources orderable
97,374 23
Install
composer require novius/laravel-nova-order-nestedset-field
Latest Version:5.1.2
PHP:^8.2
License:AGPL-3.0-or-later
Last Updated:May 13, 2026
Links: GitHub  ·  Packagist
Maintainer: novius

Nova Order Field nestedset

Travis Packagist Release Licence

A field that make your resources orderable using the laravel nestedset package.

Requirements

  • PHP >= 8.2
  • Laravel Nova >= 5.0

NOTE: These instructions are for Laravel Nova 4.0. If you are using prior version, please see the previous version's docs.

Installation

composer require novius/laravel-nova-order-nestedset-field

Configuration

Some options that you can override are available.

php artisan vendor:publish --provider="Novius\LaravelNovaOrderNestedsetField\OrderNestedsetFieldServiceProvider" --tag="config"

Usage

Step 1

Use Kalnoy\Nestedset NodeTrait and Novius\LaravelNovaOrderNestedsetField Orderable trait on your model.

Example :

use Kalnoy\Nestedset\NodeTrait;
use Novius\LaravelNovaOrderNestedsetField\Traits\Orderable;

class Foo extends Model {
    use NodeTrait;
    use Orderable;
    
    public function getLftName()
    {
        return 'left';
    }
    
    public function getRgtName()
    {
        return 'right';
    }
    
    public function getParentIdName()
    {
        return 'parent';
    }
}

Step 2

Add the field to your resource and specify order for your resources.

use Novius\LaravelNovaOrderNestedsetField\OrderNestedsetField;

class FooResource extends Resource
{       
    public function fields(Request $request)
    {
        return [
            OrderNestedsetField::make('Order'),
        ];
    }
    
    /**
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param array $orderings
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected static function applyOrderings($query, array $orderings)
    {
        return $query->orderBy('left', 'asc');
    }
}

Scoping

Imagine you have Menu model and MenuItems. There is a one-to-many relationship set up between these models. MenuItem has menu_id attribute for joining models together. MenuItem incorporates nested sets. It is obvious that you would want to process each tree separately based on menu_id attribute. In order to do so, you need to specify this attribute as scope attribute:

    protected function getScopeAttributes()
    {
        return ['menu_id'];
    }

Retrieve more information about usage on official doc.

Performances

You can enable cache to avoid performance issues in case of large tree.

By default cache is disabled.

To use cache you have to enabled it in config file with :

return [
    ...

    'cache_enabled' => true,
];

You have to clear cache on every tree updates with an observer on your Model (or directly in boot method).

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Kalnoy\Nestedset\NodeTrait;
use Novius\LaravelNovaOrderNestedsetField\Traits\Orderable;

class Foo extends Model 
{
    use NodeTrait;
    use Orderable;

    public static function boot()
    {
        parent::boot();

        if (config('nova-order-nestedset-field.cache_enabled', false)) {
            static::created(function (Theme $model) {
                $model->clearOrderableCache();
            });

            static::updated(function (Theme $model) {
                $model->clearOrderableCache();
            });

            static::deleted(function (Theme $model) {
                $model->clearOrderableCache();
            });
        }
    }
}

Override default languages files

Run:

php artisan vendor:publish --provider="Novius\LaravelNovaOrderNestedsetField\OrderNestedsetFieldServiceProvider" --tag="lang"

Lint

Run php-cs with:

composer run-script lint

Contributing

Contributions are welcome! Leave an issue on Github, or create a Pull Request.

Licence

This package is under GNU Affero General Public License v3 or (at your option) any later version.

Related Packages

spatie/nova-backup-tool

A Laravel Nova tool to backup your application.

606,582 360
maatwebsite/laravel-nova-excel

Supercharged Excel exports for Laravel Nova Resources

7,146,224 401
vyuldashev/nova-permission

A Laravel Nova tool for Spatie's Permission library.

2,643,011 429
spatie/nova-tail-tool

A Laravel Nova tool to display the application log.

129,871 119
codefocus/nestedset

Nested set implementation for Eloquent models in Laravel.

626 21

Version History

Version Released PHP Laravel License
5.1.2 ^8.2 ^10.0 | ^11.0 | ^12.0 | ^13.0 AGPL-3.0-or-later
5.1.1 ^8.2 ^10.0 | ^11.0 | ^12.0 | ^13.0 AGPL-3.0-or-later
5.1.0 ^8.2 ^10.0 | ^11.0 | ^12.0 AGPL-3.0-or-later
5.0.0 ^8.2 ^10.0 | ^11.0 AGPL-3.0-or-later
4.0.2 >=8.1 AGPL-3.0-or-later
Pre-releases (3)
Version Released PHP Laravel License
v0.3-alpha >=7.1.0 ~5.8.0|^6.0 AGPL-3.0-or-later
v0.2-alpha >=7.1.0 AGPL-3.0-or-later
v0.1-alpha >=7.1.0 AGPL-3.0-or-later