Laravel Nova - Reorder left navigation menu items

2020-08-17 05:42发布

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

6条回答
我欲成王,谁敢阻挡
2楼-- · 2020-08-17 05:42

Change /nova/resources/navigation.blade.php {{ $group }} to following:

{!! $group !!}

Now you can easily sort the groups as follows:

public static $group = '<span class="hidden">20</span>Music';

or

public static $group = '<span class="hidden">30</span>User';

Attention: You must convert special characters in the title!


With the links, it's a bit other.... First Method: dirty and ugly

You can change

{{ $resource::label() }}

to

{{ substr($resource::label(), 1) }}

Then you can sort the links by the first letter of the resource name.

  • AUser
  • BAlbum
  • CContact

Or a better Method for Links crate app/Nova/CustomResource.php:

<?php

namespace App\Nova;

use Illuminate\Support\Str;

abstract class CustomResource extends Resource
{
    public static $label = '';

    /**
     * @return string
     */
    public static function label()
    {
        if(static::$label) {
            return static::$label;
        }
        return Str::plural(Str::title(Str::snake(class_basename(get_called_class()), ' ')));
    }
}

Change /nova/resources/navigation.blade.php

{!!  $resource::label()  !!}

And in the Nova resource, extends this custom resource and You can use public static $label:

class Lyric extends CustomResource
{
    public static $label = '<span class="hidden">10</span>Lyrics';

     public static function singularLabel()
    {
        return __('Lyric');
    }

Attention: You must convert special characters in the title!

查看更多
劳资没心,怎么记你
3楼-- · 2020-08-17 05:51

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.

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

查看更多
神经病院院长
4楼-- · 2020-08-17 06:00

to sort the groups:

add this to your resources:

    public static function groupOrder() {
            return 9999999;
        }

you can overwrite it by adding it to any member resource to downgrade it's order in the navigation tree:

    public static function groupOrder() {
            return 5;
        }

add this before returning at the end of resourcemanager (i hope i shouldn't have to overwrite this at this place):

    $arrSort = [];
    foreach ($navigation as $group => $resources) {
          $resourcesGruoupOrders = [];
          foreach ($resources as $aResource) {
                $resourcesGruoupOrders[] = $aResource::groupOrder();
          }
          $arrSort[] = min($resourcesGruoupOrders);
    }
          $navigation = json_decode(json_encode($navigation), true);
          array_multisort($navigation, SORT_ASC, SORT_NUMERIC, $arrSort);


查看更多
趁早两清
5楼-- · 2020-08-17 06:06

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

查看更多
男人必须洒脱
6楼-- · 2020-08-17 06:06

There are two ways to achieve this:

  1. By setting priority to Resource
  2. Ordering Resource models in NovaServiceProvider

1. Priority Method

  • Add priority as in the following code in Resource model:
      public static $priority = 2;
    
  • Then update NovaServiceProvider like this:
    public function boot()
    {
        Nova::sortResourcesBy(function ($resource) {
            return $resource::$priority ?? 9999;
        });
    }
    

2. Ordering Resource models in NovaServiceProvider

In NovaServiceProvider, order Resource models like this:

protected function resources()
{
    Nova::resources([
        User::class,
        Post::class,
    ]);
 }
查看更多
孤傲高冷的网名
7楼-- · 2020-08-17 06:08

you can use grouping if that helps. I know it's not a 100% fix but maybe it will help a bit.

public static $group = 'Admin';
查看更多
登录 后发表回答