Skip to content
Shea Lewis edited this page Nov 16, 2015 · 1 revision

Attaching Attributes

Attaching attributes to your menu items is simple. You are able to define as many attributes as you may need to any item through a variety of consistent interfaces - utilize whichever one you think is the cleanest.

Pass attributes through the add() method

Menu::make('Example', function($menu) {
    $menu->add('Link One', ['url' => 'link-one', 'class' => 'my-super-class', 'id' => 'link-one']);
    $menu->add('Link Two', ['url' => 'link-two', 'class' => 'my-super-class', 'id' => 'link-two']);
});

In this example, both the class and id attributes will be attributed to each item.

Attach attributes via the attribute() method

You may also make use of the attribute() method to assign attributes rather than passing them through the add() method. This results is easier to read code:

Pass as an array

Menu::make('Example', function($menu) {
    $menu->add('Link One', 'link-one')->attribute(['class' => 'my-super-class', 'id' => 'link-one']);
    $menu->add('Link Two', 'link-two')->attribute(['class' => 'my-super-class', 'id' => 'link-two']);
});

Pass individually

Menu::make('Example', function($menu) {
    $menu->add('Link One', 'link-one')
        ->attribute('class', 'my-super-class')
        ->attribute('id', 'link-one');

    $menu->add('Link Two', 'link-two')
        ->attribute('class', 'my-super-class')
        ->attribute('id', 'link-two');
});

Rendering Attributes

Rendering attributes is simple within your view file. Simply echo out the item's attributes() method as a raw string, and you'll be good to go! The package will automatically generate the correct HTML syntax.

Blade

{!! $item->attributes() !!}