bdhabib / laravel-menu
Drag & Drop menu builder like WordPress for Laravel 12.x
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Language:JavaScript
Requires
- php: ^8.2
- illuminate/support: ^v12.2.0
README
Installation
- Run
composer require bdhabib/laravel-menu
- Run publish
php artisan vendor:publish --provider="Bdhabib\LaravelMenu\LaravelMenuServiceProvider"
- Configure (optional) in config/menu.php :
- CUSTOM MIDDLEWARE: You can add you own middleware
- TABLE PREFIX: By default this package will create 2 new tables named "menus" and "menu_items" but you can still add your own table prefix avoiding conflict with existing table
- TABLE NAMES If you want use specific name of tables you have to modify that and the migrations
- Custom routes If you want to edit the route path you can edit the field
- Role Access If you want to enable roles (permissions) on menu items
- Post Model You can add your own post model. Default is Post
- Category Model You can add your own category model. Default is Category
- Post Title Column You can add your own post model title column. Default is title
- Category Title Column You can add your own category model title column. Default is name
- Run migrate
php artisan migrate
DONE
Laravel Menu Usage Example - displays the UI
On your view blade file
@extends('app') @section('contents') {!! LaravelMenu::render() !!} @endsection // Recommended to Add Font Awesome CDN In Your Backend Header //maxcdn.bootstrapcdn.com/font-awesome/6.1.1/css/font-awesome.min.css //YOU MUST HAVE JQUERY LOADED BEFORE menu scripts @push('scripts') {!! LaravelMenu::scripts() !!} @endpush
Using The Model
Call the model class
use Bdhabib\LaravelMenu\Models\Menus; use Bdhabib\LaravelMenu\Models\MenuItems;
Menu Usage Example (a)
A basic two-level menu can be displayed in your blade template
Using Model Class
/* get menu by id*/ $menu = Menus::find(1); /* or by name */ $menu = Menus::where('name','Your Menu name')->first(); /* or get menu by name and the items with EAGER LOADING (RECOMENDED for better performance and less query call)*/ $menu = Menus::where('name','Your Menu name')->with('items')->first(); /*or by id */ $menu = Menus::where('id', 1)->with('items')->first(); //you can access by model result $primary_menu = $menu->items; //or you can convert it to array $primary_menu = $menu->items->toArray();
or Using helper
// Using Helper $primary_menu = LaravelMenu::getByName('Primary'); //return array
Menu Usage Example (b)
Now inside your blade template file place the menu using this simple example
<nav class="" id="navbar"> <div class="navbar__menu container"> <ul> @if ($primary_menu) @foreach ($primary_menu as $menu) <li> <a href="{{ $menu['link'] }}" title="{{ $menu['label'] }}">{{ $menu['label'] }}</a> @if ($menu['child']) <ul class="sub-menu"> @foreach ($menu['child'] as $child) <li class=""><a href="{{ $child['link'] }}" title="">{{ $child['label'] }}</a> </li> @endforeach </ul><!-- /.sub-menu --> @endif </li> @endforeach @endif </div> </nav>
HELPERS
Get Menu Items By Menu ID
use Bdhabib\LaravelMenu\Facades\LaravelMenu; ... /* Parameter: Menu ID Return: Array */ $menuList = LaravelMenu::get(1);
Get Menu Items By Menu Name
In this example, you must have a menu named Admin
use Bdhabib\LaravelMenu\Facades\LaravelMenu; ... /* Parameter: Menu ID Return: Array */ $menuList = LaravelMenu::getByName('Admin');
Customization
you can edit the menu interface in resources/views/vendor/laravel-menu/menu.blade.php
Credits
- wmenu laravel package menu like wordpress
Compatibility
- Tested with laravel 11.x
- Work only laravel 11.x