blimundo/laravel-menu is a Laravel package for menu management for laravel applications.
It currently has 1 GitHub stars and 42 downloads on Packagist (latest version 2.0.0).
Install it with composer require blimundo/laravel-menu.
Discover more Laravel packages by blimundo
or browse all Laravel packages to compare alternatives.
Last updated
Menu management in Laravel made simple and painless. This package does not provide any UI, focusing only on functionality to store and display the menu.
Items can be displayed in different languages thanks to the spatie/laravel-translatable package.
This package requires Laravel 8 or higher, PHP 8 or higher, and a database that supports json fields and MySQL compatible functions.
You can install the package via composer:
composer require blimundo/laravel-menu
The package will automatically register itself.
After install you can create the menus table by running the migrations:
php artisan migrate
A MenuGenerator alias will be available to application.
use Blimundo\Menu\Builder;
Builder::add('Home')->order(1)->icon('mdi mdi-home')->url('HomeController@show')->create();
Builder::add(['en' => 'Settings', 'pt' => 'Configurações'])
->order(9)
->icon('mdi mdi-cog')
->items(function () {
Builder::add(['en' => 'Roles', 'pt' => 'Funções'])
->order(1)
->icon('mdi mdi-account-group')
->url('role.index')
->gates('can_view_roles')
->create();
Builder::add(['en' => 'Users', 'pt' => 'Utilizadores'])
->order(2)
->icon('mdi mdi-account')
->url('user.index')
->gates('can_view_users')
->create();
});
dd(MenuGenerator::generate());
/* Result:
array:2 [
"Home" => array:5 [
"icon" => "mdi mdi-home"
"label" => "Home"
"link" => "localhost:8000/home"
"level" => 1
"has_items" => false
]
"Settings" => array:6 [
"icon" => "mdi mdi-cog"
"label" => "Settings"
"link" => "#"
"level" => 1
"has_items" => true
"items" => array:2 [
"Roles" => array:5 [
"icon" => "mdi mdi-account-group"
"label" => "Roles"
"link" => "localhost:8000/role"
"level" => 2
"has_items" => false
]
"Users" => array:5 [
"icon" => "mdi mdi-account"
"label" => "Users"
"link" => "localhost:8000/user"
"level" => 2
"has_items" => false
]
]
]
*/
To create and persist the menu in the database, you can use the Builder class. In the example below, two menu entries are created:
use Blimundo\Menu\Builder;
Builder::add('Home')->create();
Builder::add('Help')->create();
To define multiple languages, just pass an array to the add() method, where the key is the language code. Internally the package uses spatie/laravel-translatable to manage translations.
Builder::add(['en' => 'Help', 'pt' => 'Ajuda'])->create();
You have 3 options to generate urls:
Builder::add('Google')->url('https://google.com')->create();
Builder::add('Add User')->route('users.create')->create();
Builder::add('Import User')->action('\App\Http\Controllers\UserController@import')->create();
To associate an icon to the menu just call the icon() function
Builder::add('Help')->icon('mdi mdi-help')->create();
To set the item order just call the order() function.
Builder::add('Help')->order(2)->create();
Items with the same order (or that have no defined order) are sorted alphabetically according to the current language.
Sometimes we want the menu to appear only if the user has certain permissions. This package makes this a breeze. Just call the gates() function with the name of the gate. When generating the menu, the package tests the gate and if it passes the menu is included.
Builder::add('Add User')->gates('can_add_user')->create();
If you need to test more than one gate, just pass a list. The menu is only displayed if all gates return true.
Builder::add('Add User')->gates('can_add_user', 'can_import_user')->create();
To create a submenu call the items() function, passing a callback. All menus created within the callback will be associated with the menu.
Builder::add('Settings')->items(function () {
Builder::add('Roles')->create();
Builder::add('Users')->create();
});
The Laravel Menu is open-sourced software licensed under the MIT license.