Menu

Introduction

The Menus are lists of links that appear on your application.

Registering menus allow for creating easily customizable menus areas, which can be managed inside Appearance > Menus administration panel.

Basics of menus

All application menus should be defined inside app\Support\menus.php file. This file is included on application bootstrap by the App\Providers\AppServiceProvider class.

Creating menu

Registering menus are really simple. All you have to do is to call create method on Menu facade.

Menu::create('primary');
Arguments

Create method accepts options as the second argument.

Menu::create('primary', [
    'title' => ['Main menu'],
]);

List of available options:

Option name Default value Description
title [] Menu title displayed in the admin

Getting menu

Calling get method will return previously registered menu instance.

Menu::get('primary');

Checking if menu is in use

You can check if menu contains any items with isActive method.

@if(Menu::get('primary')->isActive())
    // ...menu have items
@endif

Rendering menu in templates

Grab all menu items with items method. This will return a collection of Assely\Adapter\Menu instances.

<!-- resources/views/index.blade.php -->

<div class="menu--primary">
    @include('menu.nav', [
        'items' => Menu::get('primary')->items()
    ])
</div>

Now, you only need to loop through items in a @foreach statement.

<!-- resources/views/menu/nav.blade.php -->

<ul>
    @foreach($items as $item)
        <li>
            <a href="{{ $item->link }}">{{ $item->title }}</a>

            <!--
            If menu item have children items,
            recursively include this view.
            -->
            @if($item->hasChildren())
                @include('menu.nav', [
                    'items' => $item->children()
                ])
            @endif
        </li>
    @endforeach
</ul>