$label - group name
$items - array of menu components
$icon - icon for the group.
namespace App\Providers;
use MoonShine\Menu\MenuGroup;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
use MoonShine\Resources\MoonShineUserResource;
use MoonShine\Resources\MoonShineUserRoleResource;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuGroup::make('System', [
MenuItem::make('Admins', new MoonShineUserResource()),
MenuItem::make('Roles', new MoonShineUserRoleResource()),
])
];
}
//...
}
namespace App\Providers;
use MoonShine\Menu\MenuGroup;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
use MoonShine\Resources\MoonShineUserResource;
use MoonShine\Resources\MoonShineUserRoleResource;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuGroup::make('System', [
MenuItem::make('Admins', new MoonShineUserResource()),
MenuItem::make('Roles', new MoonShineUserRoleResource()),
])
];
}
//...
}
You can also add elements to a group using method setItems()
setItems(iterable $items)
setItems(iterable $items)
setItems(iterable $items)
namespace App\Providers;
use MoonShine\Menu\MenuGroup;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
use MoonShine\Resources\MoonShineUserResource;
use MoonShine\Resources\MoonShineUserRoleResource;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuGroup::make('System')->setItems([
MenuItem::make('Admins', new MoonShineUserResource()),
MenuItem::make('Roles', new MoonShineUserRoleResource()),
])
];
}
//...
}
namespace App\Providers;
use MoonShine\Menu\MenuGroup;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
use MoonShine\Resources\MoonShineUserResource;
use MoonShine\Resources\MoonShineUserRoleResource;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuGroup::make('System')->setItems([
MenuItem::make('Admins', new MoonShineUserResource()),
MenuItem::make('Roles', new MoonShineUserRoleResource()),
])
];
}
//...
}
To create a multi-level menu, groups can be nested.
Menu items can be visually divided using MoonShine\Menu\MenuDivider.
MenuDivider::make(Closure|string $label = '')
MenuDivider::make(Closure|string $label ='')
MenuDivider::make(Closure|string $label = '')
namespace App\Providers;
use App\MoonShine\Resources\ArticleResource;
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Menu\MenuDivider;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuItem::make('Admins', new MoonShineUserResource()),
MenuDivider::make(),
MenuItem::make('Roles', new MoonShineUserRoleResource()),
];
}
//...
}
namespace App\Providers;
use App\MoonShine\Resources\ArticleResource;
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Menu\MenuDivider;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuItem::make('Admins', new MoonShineUserResource()),
MenuDivider::make(),
MenuItem::make('Roles', new MoonShineUserRoleResource()),
];
}
//...
}
You can use text as a delimiter; to do this, you need to pass it to the make() method.
namespace App\Providers;
use App\MoonShine\Resources\ArticleResource;
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Menu\MenuDivider;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuItem::make('Admins', new MoonShineUserResource()),
MenuDivider::make('Divider'),
MenuItem::make('Roles', new MoonShineUserRoleResource()),
];
}
//...
}
namespace App\Providers;
use App\MoonShine\Resources\ArticleResource;
use App\MoonShine\Resources\CategoryResource;
use MoonShine\Menu\MenuDivider;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuItem::make('Admins', new MoonShineUserResource()),
MenuDivider::make('Divider'),
MenuItem::make('Roles', new MoonShineUserRoleResource()),
];
}
//...
}
You can set an icon for a menu item and a group. This can be accomplished in several ways.
Via parameter
The icon can be set by passing the name as the third parameter in static method make().
namespace App\Providers;
use MoonShine\Menu\MenuGroup;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
use MoonShine\Resources\MoonShineUserResource;
use MoonShine\Resources\MoonShineUserRoleResource;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuGroup::make('System', [
MenuItem::make('Admins', new MoonShineUserResource(), 'heroicons.outline.users'),
MenuItem::make('Roles', new MoonShineUserRoleResource(), 'heroicons.hashtag'),
])
];
}
//...
}
namespace App\Providers;
use MoonShine\Menu\MenuGroup;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
use MoonShine\Resources\MoonShineUserResource;
use MoonShine\Resources\MoonShineUserRoleResource;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuGroup::make('System', [
MenuItem::make('Admins', new MoonShineUserResource(), 'heroicons.outline.users'),
MenuItem::make('Roles', new MoonShineUserRoleResource(), 'heroicons.hashtag'),
])
];
}
//...
}
Via method
Use method icon().
icon(string $icon)
icon(string $icon)
icon(string $icon)
namespace App\Providers;
use MoonShine\Menu\MenuGroup;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
use MoonShine\Resources\MoonShineUserResource;
use MoonShine\Resources\MoonShineUserRoleResource;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuGroup::make('System', [
MenuItem::make('Admins', new MoonShineUserResource())
->icon('heroicons.outline.users'),
MenuItem::make('Roles', new MoonShineUserRoleResource())
->icon('heroicons.hashtag'),
])
->icon('heroicons.cog')
];
}
//...
}
namespace App\Providers;
use MoonShine\Menu\MenuGroup;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
use MoonShine\Resources\MoonShineUserResource;
use MoonShine\Resources\MoonShineUserRoleResource;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuGroup::make('System', [
MenuItem::make('Admins', new MoonShineUserResource())
->icon('heroicons.outline.users'),
MenuItem::make('Roles', new MoonShineUserRoleResource())
->icon('heroicons.hashtag'),
])
->icon('heroicons.cog')
];
}
//...
}
Via attribute
The menu item will display an icon if the class ModelResource, Page or Resource the Icon attribute is specified and the icon is not overridden in other ways.
namespace MoonShine\Resources;
#[Icon('heroicons.outline.users')]
class MoonShineUserResource extends ModelResource
{
//...
}
namespaceMoonShine\Resources;
#[Icon('heroicons.outline.users')]
classMoonShineUserResourceextendsModelResource
{
//...
}
namespace MoonShine\Resources;
#[Icon('heroicons.outline.users')]
class MoonShineUserResource extends ModelResource
{
//...
}
For more detailed information, please refer to the section Icons.
It is also possible to add an icon to a menu item.
Via menu item
To add a badge to a menu item, use method badge(), which takes a closure as a parameter.
badge(Closure $callback)
badge(Closure $callback)
badge(Closure $callback)
namespace App\Providers;
use App\Models\Comment;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuItem::make('Comments', new CommentResource())
->badge(fn() => Comment::count())
];
}
//...
}
namespace App\Providers;
use App\Models\Comment;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuItem::make('Comments', new CommentResource())
->badge(fn() => Comment::count())
];
}
//...
}
Through a class method
For ModelResource, Page or Resource There is an alternative way to set the badge - method getBadge().
namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
//...
public function getBadge(): string
{
return 'new';
}
//...
}
namespaceApp\MoonShine\Resources;
useApp\Models\Post;
useMoonShine\Resources\ModelResource;
classPostResourceextendsModelResource
{
//...
publicfunctiongetBadge():string
{
return'new';
}
//...
}
namespace App\MoonShine\Resources;
use App\Models\Post;
use MoonShine\Resources\ModelResource;
class PostResource extends ModelResource
{
//...
public function getBadge(): string
{
return 'new';
}
//...
}
To translate menu items, you need to pass the translation key as the name and add method translatable()
translatable(string $key = '')
translatable(string $key ='')
translatable(string $key = '')
namespace App\Providers;
use App\Models\Comment;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuItem::make('menu.Comments', new CommentResource())
->translatable()
// or
MenuItem::make('Comments', new CommentResource())
->translatable('menu')
];
}
//...
}
namespace App\Providers;
use App\Models\Comment;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuItem::make('menu.Comments', new CommentResource())
->translatable()
// or
MenuItem::make('Comments', new CommentResource())
->translatable('menu')
];
}
//...
}
You can use Laravel's translation tools to translate menu labels.
namespace App\Providers;
use App\Models\Comment;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuItem::make('Comments', new CommentResource())
->badge(fn() => __('menu.badge.new'))
];
}
//...
}
namespace App\Providers;
use App\Models\Comment;
use App\MoonShine\Resources\CommentResource;
use MoonShine\Menu\MenuItem;
use MoonShine\Providers\MoonShineApplicationServiceProvider;
class MoonShineServiceProvider extends MoonShineApplicationServiceProvider
{
protected function menu(): array
{
return [
MenuItem::make('Comments', new CommentResource())
->badge(fn() => __('menu.badge.new'))
];
}
//...
}