Menu is the foundation for navigation in the admin panel, so we have tried to create a flexible system that allows you to fully customize the menu for different pages and users.
The navigation menu is configured in a class that extends MoonShine\Laravel\Layouts\AppLayout through the menu() method.
During the installation of the admin panel, depending on the configurations you choose, a class App\MoonShine\Layouts\MoonShineLayout will be created, which already contains the menu() method.
In the future, if necessary, you can create other Layouts for specific pages.
To add a menu item, you need to use the class MoonShine\Menu\MenuItem and its static method make().
namespace App\MoonShine\Layouts;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\Laravel\Resources\MoonShineUserResource;
use MoonShine\Laravel\Resources\MoonShineUserRoleResource;
use MoonShine\MenuManager\MenuGroup;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuGroup::make('System', [
MenuItem::make('Admins', MoonShineUserResource::class),
MenuItem::make('Roles', MoonShineUserRoleResource::class),
])
];
}
}
namespace App\MoonShine\Layouts;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\Laravel\Resources\MoonShineUserResource;
use MoonShine\Laravel\Resources\MoonShineUserRoleResource;
use MoonShine\MenuManager\MenuGroup;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuGroup::make('System', [
MenuItem::make('Admins', MoonShineUserResource::class),
MenuItem::make('Roles', MoonShineUserRoleResource::class),
])
];
}
}
You can also add items to the group using the setItems() method.
setItems(iterable $items)
setItems(iterable $items)
setItems(iterable $items)
namespace App\MoonShine\Layouts;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\Laravel\Resources\MoonShineUserResource;
use MoonShine\Laravel\Resources\MoonShineUserRoleResource;
use MoonShine\MenuManager\MenuGroup;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuGroup::make('System')->setItems([
MenuItem::make('Admins', MoonShineUserResource::class),
MenuItem::make('Roles', MoonShineUserRoleResource::class),
])
];
}
}
namespace App\MoonShine\Layouts;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\Laravel\Resources\MoonShineUserResource;
use MoonShine\Laravel\Resources\MoonShineUserRoleResource;
use MoonShine\MenuManager\MenuGroup;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuGroup::make('System')->setItems([
MenuItem::make('Admins', MoonShineUserResource::class),
MenuItem::make('Roles', MoonShineUserRoleResource::class),
])
];
}
}
To create a multi-level menu, groups can be nested.
namespace App\MoonShine\Layouts;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\Laravel\Resources\MoonShineUserResource;
use MoonShine\Laravel\Resources\MoonShineUserRoleResource;
use MoonShine\MenuManager\MenuDivider;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuItem::make('Admins', MoonShineUserResource::class),
MenuDivider::make(),
MenuItem::make('Roles', MoonShineUserRoleResource::class)
];
}
}
namespace App\MoonShine\Layouts;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\Laravel\Resources\MoonShineUserResource;
use MoonShine\Laravel\Resources\MoonShineUserRoleResource;
use MoonShine\MenuManager\MenuDivider;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuItem::make('Admins', MoonShineUserResource::class),
MenuDivider::make(),
MenuItem::make('Roles', MoonShineUserRoleResource::class)
];
}
}
An icon can be assigned to both a menu item and a group. This can be implemented in several ways.
Through parameter
An icon can be set by passing the name as the third parameter in the static method make().
namespace App\MoonShine\Layouts;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\Laravel\Resources\MoonShineUserResource;
use MoonShine\Laravel\Resources\MoonShineUserRoleResource;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuItem::make('Admins', MoonShineUserResource::class, 'users'),
MenuItem::make('Roles', MoonShineUserRoleResource::class, 'hashtag')
];
}
}
namespace App\MoonShine\Layouts;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\Laravel\Resources\MoonShineUserResource;
use MoonShine\Laravel\Resources\MoonShineUserRoleResource;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuItem::make('Admins', MoonShineUserResource::class, 'users'),
MenuItem::make('Roles', MoonShineUserRoleResource::class, 'hashtag')
];
}
}
namespace App\MoonShine\Layouts;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\Laravel\Resources\MoonShineUserResource;
use MoonShine\Laravel\Resources\MoonShineUserRoleResource;
use MoonShine\MenuManager\MenuGroup;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuGroup::make('System', [
MenuItem::make('Admins', MoonShineUserResource::class)
->icon('users'),
MenuItem::make('Roles', MoonShineUserRoleResource::class)
->icon(svg('path-to-icon-pack')->toHtml(), custom: true),
])
->icon('cog', path: 'icons')
];
}
}
Through attribute
An icon will be displayed for the menu item if the ModelResource, Page, or Resource class has the Icon attribute set and the icon has not been overridden by other means.
namespace MoonShine\Resources;
#[Icon('users')]
class MoonShineUserResource extends ModelResource
{
//...
}
namespaceMoonShine\Resources;
#[Icon('users')]
classMoonShineUserResourceextendsModelResource
{
//...
}
namespace MoonShine\Resources;
#[Icon('users')]
class MoonShineUserResource extends ModelResource
{
//...
}
For more detailed information, refer to the section Icons.
namespace App\MoonShine\Layouts;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuItem::make('Comments', CommentResource::class)
->badge(fn() => Comment::count())
];
}
}
namespace App\MoonShine\Layouts;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuItem::make('Comments', CommentResource::class)
->badge(fn() => Comment::count())
];
}
}
To translate menu items, you need to pass the translation key as the name and add the translatable() method.
translatable(string $key = '')
translatable(string $key ='')
translatable(string $key = '')
namespace App\MoonShine\Layouts;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuItem::make('menu.Comments', CommentResource::class)
->translatable()
// or
MenuItem::make('Comments', CommentResource::class)
->translatable('menu')
];
}
}
namespace App\MoonShine\Layouts;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuItem::make('menu.Comments', CommentResource::class)
->translatable()
// or
MenuItem::make('Comments', CommentResource::class)
->translatable('menu')
];
}
}
You can use Laravel's translation features for translating menu badges.
namespace App\MoonShine\Layouts;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuItem::make('Comments', CommentResource::class)
->badge(fn() => __('menu.badge.new'))
];
}
}
namespace App\MoonShine\Layouts;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Laravel\Layouts\AppLayout;
use MoonShine\MenuManager\MenuItem;
final class MoonShineLayout extends AppLayout
{
//...
protected function menu(): array
{
return [
MenuItem::make('Comments', CommentResource::class)
->badge(fn() => __('menu.badge.new'))
];
}
}