PHP code example of bdacademy / laravel-menu

1. Go to this page and download the library: Download bdacademy/laravel-menu library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bdacademy / laravel-menu example snippets


composer 

php artisan vendor:publish --provider="Bdacademy\LaravelMenu\LaravelMenuServiceProvider"

php artisan migrate

@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

use Bdacademy\LaravelMenu\Models\Menus;
use Bdacademy\LaravelMenu\Models\MenuItems;



/* 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();


// Using Helper
$primary_menu = LaravelMenu::getByName('Primary'); //return array


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


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

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