balajidharma/laravel-menu

Laravel database menu manager

v2.0.2 2024-08-01 00:42 UTC

This package is auto-updated.

Last update: 2024-11-05 00:59:48 UTC


README

Create database based Menu and Menu items to your Laravel projects.

Total Downloads Latest Stable Version License

Table of Contents

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;