Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Nova - Reorder left navigation menu items

In default the ordering of left menu items is in alphabetical order.

My client wants to order those menus manually. Any idea how to make it possible?

enter image description here

Go to answer

like image 736
Vineeth Vijayan Avatar asked Nov 21 '18 05:11

Vineeth Vijayan


2 Answers

You can do it in

App\Providers\NovaServiceProvider.php

add a method resources() and register the resources manually like

 protected function resources()
    {
        Nova::resources([
            User::class,
            Post::class,
        ]);
    }

Alternate

There is another way mentioned in this gist, this seems good too, but official documentation has no mention of it yet.

Resource

<?php

namespace App\Nova;

class User extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = 'App\\User';

    /**
     * Custom priority level of the resource.
     *
     * @var int
     */
    public static $priority = 1;

    // ...
}

and in NovaServiceProvider

<?php

namespace App\Providers;

use Laravel\Nova\Nova;
use Laravel\Nova\Cards\Help;
use Illuminate\Support\Facades\Gate;
use Laravel\Nova\NovaApplicationServiceProvider;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
   /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        Nova::sortResourcesBy(function ($resource) {
            return $resource::$priority ?? 99999;
        });
    }
}

This way you set priority of resource and based on priority you render the resource.

like image 71
Prafulla Kumar Sahu Avatar answered Sep 22 '22 18:09

Prafulla Kumar Sahu


A cleaner way and tested on latest Nova 3.x. Also, this has been added to Nova since version 2.10+ All you need to do is add a static property on your nova classes. For example Clients will be:

/**
 * The side nav menu order.
 *
 * @var int
 */
public static $priority = 2;

Then after that you can use the NovaServiceProvider to tell nova to use your custom ordering. You can place the code in the boot method

public function boot()
{
    Nova::sortResourcesBy(function ($resource) {
        return $resource::$priority ?? 9999;
    });
}

**Reference Nova Private Repo

like image 43
zeidanbm Avatar answered Sep 20 '22 18:09

zeidanbm