An admin interface for Laravel to easily add, edit or remove Menus.
- In your terminal:
composer require smartystudio/laravelmenu
Step 2 & 3 are optional if you are using laravel 5.5
- If your Laravel version does not have package autodiscovery then add the service provider to your config/app.php file:
SmartyStudio\LaravelMenu\MenuServiceProvider::class,
- Add facade in the file config/app.php (optional on laravel 5.5):
'Menu' => SmartyStudio\LaravelMenu\Facades\Menu::class,
- Publish the config file & run the migrations.
php artisan vendor:publish --provider="SmartyStudio\LaravelMenu\MenuServiceProvider"
- Configure (optional) in
config/laravelmenu.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
- Run the database migrations:
php artisan migrate
DONE!
On your view blade file
@extends('app')
@section('contents')
{!! Menu::render() !!}
@endsection
You must have jQuery loaded before menu scripts
@push('scripts')
{!! Menu::scripts() !!}
@endpush
Call the model class:
use SmartyStudio\LaravelMenu\Models\Menu;
use SmartyStudio\LaravelMenu\Models\MenuItem;
A basic two-level menu can be displayed in your blade template.
// Get menu by ID
$menu = Menu::find(1);
// Get menu by Name
$menu = Menu::where('name','Test Menu')->first();
/**
* Get menu by Name and the Items with eager loading.
* This is RECOMENDED for better performance and less query calls.
*/
$menu = Menu::where('name','Test Menu')->with('items')->first();
// Get menu by ID
$menu = Menu::where('id', 1)->with('items')->first();
// Access by Model result
$public_menu = $menu->items;
// Convert it to Array
$public_menu = $menu->items->toArray();
// Using Helper
$public_menu = Menu::getByName('Public'); // return array
Now inside your blade template file, place the menu using this simple example:
<div class="nav-wrap">
<div class="btn-menu">
<span></span>
</div><!-- // mobile menu button -->
<nav id="mainnav" class="mainnav">
@if($public_menu)
<ul class="menu">
@foreach($public_menu as $menu)
<li class="">
<a href="{{ $menu['link'] }}" title="">{{ $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
<!-- empty -->
@endif
</ul><!-- /.menu -->
</nav><!-- /#mainnav -->
</div><!-- /.nav-wrap -->
use SmartyStudio\LaravelMenu\Facades\Menu;
...
/**
* Parameter: Menu ID
* Return: Array
*/
$menuList = Menu::get(1);
In this example, you must have a menu named Admin
use SmartyStudio\LaravelMenu\Facades\Menu;
...
/**
* Parameter: Menu ID
* Return: Array
*/
$menuList = Menu::getByName('Admin');
You can publish and edit the menu layout in resources/views/vendor/smartystudio/laravelmenu/menu.blade.php
Please see CHANGELOG for more information what has changed recently.
// TODO
Please see CONTRIBUTING for details.
If you discover any security related issues, please email us instead of using the issue tracker.
- Martin Nestorov - Web Developer @ Smarty Studio.
- All Contributors
The SmartyStudio\LaravelMenu is open-source software licensed under the MIT license.