bdhabib/laravel-menu

Drag & Drop menu builder like WordPress for Laravel 12.x

2.2 2025-03-17 19:31 UTC

This package is auto-updated.

Last update: 2025-04-17 19:57:25 UTC


README

Laravel drag and drop menu

Installation

  1. Run
composer require bdhabib/laravel-menu
  1. Run publish
php artisan vendor:publish --provider="Bdhabib\LaravelMenu\LaravelMenuServiceProvider"
  1. 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
  1. 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