Skip to content

Instantly share code, notes, and snippets.

@jbrooksuk
Created February 18, 2020 12:12
Show Gist options
  • Save jbrooksuk/c7c1ab6ea094a29d7dbb1fbdb17d05e1 to your computer and use it in GitHub Desktop.
Save jbrooksuk/c7c1ab6ea094a29d7dbb1fbdb17d05e1 to your computer and use it in GitHub Desktop.
Using Nova::sortResourcesBy
<?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
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
parent::boot();
}
/**
* Register the Nova routes.
*
* @return void
*/
protected function routes()
{
Nova::routes()
->withAuthenticationRoutes()
->withPasswordResetRoutes()
->register();
}
/**
* Register the Nova gate.
*
* This gate determines who can access Nova in non-local environments.
*
* @return void
*/
protected function gate()
{
Gate::define('viewNova', function ($user) {
return in_array($user->email, [
'[email protected]',
]);
});
}
/**
* Get the cards that should be displayed on the Nova dashboard.
*
* @return array
*/
protected function cards()
{
return [
new Help,
];
}
/**
* Get the tools that should be listed in the Nova sidebar.
*
* @return array
*/
public function tools()
{
return [];
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
Nova::sortResourcesBy(function ($resource) {
return $resource::$priority ?? 99999;
});
}
}
<?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;
// ...
}
@trippo
Copy link

trippo commented Nov 13, 2020

This not work for groups
A possible solution is prepend a number in all group name functions like:

public static function group(){
    return "1. First Group";
}

@eugenefvdm
Copy link

I came here from this Stack post where people seems to have lifted the info: https://stackoverflow.com/questions/53405577/laravel-nova-reorder-left-navigation-menu-items

Nice to see a gist by a Laravel employee

What I would like to know is also how to arrange groups.

@ibrahim-999
Copy link

@trippo i have tried your solution but when i came to the group 10 it does not sort correctly,
do you have any other solution.

@trippo
Copy link

trippo commented Jul 18, 2022

@trippo i have tried your solution but when i came to the group 10 it does not sort correctly, do you have any other solution.

Use 01 02...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment