Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
/ laravelmenu Public archive

Drag and drop menu generator for Laravel.

License

Notifications You must be signed in to change notification settings

smartystudio/laravelmenu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Drag and Drop menu generator for Laravel

An admin interface for Laravel to easily add, edit or remove Menus.

Install

  1. In your terminal:
composer require smartystudio/laravelmenu

Step 2 & 3 are optional if you are using laravel 5.5

  1. If your Laravel version does not have package autodiscovery then add the service provider to your config/app.php file:
SmartyStudio\LaravelMenu\MenuServiceProvider::class,
  1. Add facade in the file config/app.php (optional on laravel 5.5):
'Menu' => SmartyStudio\LaravelMenu\Facades\Menu::class,
  1. Publish the config file & run the migrations.
php artisan vendor:publish --provider="SmartyStudio\LaravelMenu\MenuServiceProvider"
  1. 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
  1. Run the database migrations:
php artisan migrate

DONE!

Menu Builder Usage Example - displays the builder

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

Using the Model

Call the model class:

use SmartyStudio\LaravelMenu\Models\Menu;
use SmartyStudio\LaravelMenu\Models\MenuItem;

Menu Usage Example (a)

A basic two-level menu can be displayed in your blade template.

Using Model class

// 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 class

// Using Helper 
$public_menu = Menu::getByName('Public'); // return array

Menu Usage Example (b)

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 -->

HELPERS

Get Menu Items By Menu ID

use SmartyStudio\LaravelMenu\Facades\Menu;
...
/**
 * Parameter: Menu ID
 * Return:    Array
 */
$menuList = Menu::get(1);

Get Menu Items By Menu Name

In this example, you must have a menu named Admin

use SmartyStudio\LaravelMenu\Facades\Menu;
...
/**
 * Parameter: Menu ID
 * Return:    Array
 */
$menuList = Menu::getByName('Admin');

Customization of the menu

You can publish and edit the menu layout in resources/views/vendor/smartystudio/laravelmenu/menu.blade.php

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

// TODO

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email us instead of using the issue tracker.

Credits

  • Martin Nestorov - Web Developer @ Smarty Studio.
  • All Contributors

License

The SmartyStudio\LaravelMenu is open-source software licensed under the MIT license.