balajidharma / laravel-menu
Laravel database menu manager
Installs: 8 634
Dependents: 2
Suggesters: 0
Security: 0
Stars: 14
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- illuminate/support: ^8.71|^9.0|^10.0|^11.0
README
Create database based Menu and Menu items to your Laravel projects.
Table of Contents
- Installation
- Demo
- Create Menu
- Create Menu Item
- Create multiple Menu Items
- Menu Tree
- Menu Link Tokens
Installation
- Install the package via composer
composer require balajidharma/laravel-menu
- Publish the migration and the config/menu.php config file with
php artisan vendor:publish --provider="BalajiDharma\LaravelMenu\MenuServiceProvider"
- Run the migrations
php artisan migrate
Demo
The "Basic Laravel Admin Penel" starter kit come with Laravel Menu
Create Menu
use BalajiDharma\LaravelMenu\Models\Menu; Menu::create([ 'name' => 'Main Menu', 'machine_name' => 'main_menu', 'description' => 'Site main menu', ]);
Create Menu Item
use BalajiDharma\LaravelMenu\Models\Menu; use BalajiDharma\LaravelMenu\Models\MenuItem; $menu = Menu::create([ 'name' => 'Main Menu', 'machine_name' => 'main_menu', 'description' => 'Site main menu' ]); $menu->menuItems()->create([ 'name' => 'Home', 'uri' => '/', 'enabled' => 1, 'parent_id' => null, 'weight' => 0 ]);
Create multiple Menu Items
$menu = Menu::create([ 'name' => 'Admin', 'machine_name' => 'admin', 'description' => 'Admin Menu', ]); $menu_items = [ [ 'name' => 'Dashboard', 'uri' => '/dashboard', 'enabled' => 1, 'weight' => 0, ], [ 'name' => 'Permissions', 'uri' => '/<admin>/permission', 'enabled' => 1, 'weight' => 1, ], [ 'name' => 'Roles', 'uri' => '/<admin>/role', 'enabled' => 1, 'weight' => 2, ], [ 'name' => 'Users', 'uri' => '/<admin>/user', 'enabled' => 1, 'weight' => 3, ], [ 'name' => 'Menus', 'uri' => '/<admin>/menu', 'enabled' => 1, 'weight' => 4, ] ]; $menu->menuItems()->createMany($menu_items);
Menu Tree
- Get menu tree by using menu id
use BalajiDharma\LaravelMenu\Models\MenuItem; $items = (new MenuItem)->toTree($menu->id);
- Get menu tree by using menu machine name
use BalajiDharma\LaravelMenu\Models\Menu; $items = Menu::getMenuTree('admin');
Menu Link Tokens
- Enter
<admin>
to add admin prefix to the link. - Enter
<nolink>
for non link menu.
Get the generated uri on link
attribute
use BalajiDharma\LaravelMenu\Models\MenuItem; MenuItem::find(1)->link;