This is a useful package for building menus in your Laravel application, and it can help simplify the process of creating and managing menus in your application.
You can install the package via composer:
composer require salahhusa9/laravel-menu
You can publish the config file with:
php artisan vendor:publish --tag="menu-config"
Optionally, you can publish the views using
php artisan vendor:publish --tag="menu-views"
You can create menu in your application in any place you want, but the best place is in the AppServiceProvider.php file in the boot() method.
use SalahHusa9\Menu\Facades\Menu;
public function boot()
{
Menu::add('test', 'route.name', 'fa fa-home');
}
Add items to the menu using the add() method. You can chain multiple add() calls to add multiple items.
Menu::add('test')
->add('test2');
To create a submenu, call addSubmenu() on a menu item and add items to the submenu using the add() method.
Menu::add('test')
->add('test2')
->addSubmenu('test3', function ($submenu) {
$submenu->add('test4');
})
->add('test5');
To render the menu, use the <x-menu />
blade component.
<x-menu />
Icons can be added to the menu items by passing the icon parameter to the add() method.
Menu::add('test', 'route.name', ['icon' => 'fa fa-home']);
You can also add a id and class to the menu item by passing the id and class parameters to the add() method.
Menu::add('test', 'route.name',['class' => 'customClass', 'id' => 'customId']);
You can also add a target to the menu item by passing the target parameter to the add() method.
Menu::add('test', 'route.name', ['target' => '_blank']);
You can also add a badge to the menu item by passing the badgeClass and badgeName parameters to the add() method.
Menu::add('test', 'route.name', ['badgeClass' => 'badge badge-success', 'badgeName' => 'New']);
You can also
// old:
if (auth()->user()->can('gateName')){
Menu::add('test', 'route.name');
}
// new:
Menu::add('test', 'route.name', ['gateName' => 'gateName']);
You can customize the menu view by publishing the views using
php artisan vendor:publish --tag="menu-views"
You can create multiple menus in your application:
Menu::make('sidebar', function ($menu) {
$menu->add('test', 'route.name');
});
Menu::make('main', function ($menu) {
$menu->add('test', 'route.name');
});
For render the menu, use the <x-menu for="sidebar" />
blade component.
You can publish the config file with:
php artisan vendor:publish --tag="menu-config"
and you can change the defult menu Classes of Ul and Li and the active class
return [
"ul_class" => "menu-inner py-1", // default menu class
"ul_sub_menu_class" => "menu-sub", // default submenu class
"li_class" => "menu-item", // default menu item class
"li_sub_menu_class" => "menu-item", // default submenu item class
"li_sub_menu_open_class" => "menu-item active open", // default submenu item class when open
"a_class" => "menu-link", // default menu link class
"a_sub_menu_class" => "menu-link menu-toggle", // default submenu link class
"icon_class" => "menu-icon", // default menu icon class
"li_active_class" => "active", // default active class of li
"a_active_class" => "active", // default active class of a
"badge_class" => "badge rounded-pill ms-auto", // default badge class
];
Each Item accept this parames :
add(
$name,
$routeName = null,
$options = [],
)
addSubmenu(
$name,
callback $callbackOfSubmenu,
$options
)
There is other functions that you can used :
Menu::getMenuAsJson(); // return the menu as json
Menu::renderAsJson(); // return the menu as json
Menu::renderAsHtml(); // return the menu as html
See the open issues for a list of proposed features (and known issues).
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.