Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Menu Laravel Package

spatie/menu

Fluent, extensible menu builder for Laravel. Compose navigation with a clean API, render as HTML, and customize output via presenters and macros. Supports active state handling, links, submenus, and easy integration with Blade and your app’s routing.

View on GitHub
Deep Wiki
Context7

title: Manipulating Items weight: 2

There are three methods to manipulate items in a menu:

  • each: Goes over all existing items and applies a manipulation
  • registerFilter: Registers a manipulation that will be applied to all items added afterwards
  • applyToAll: Applies a manipulation to all existing and all future items

Typehinting Callables

All methods require a callable as their first and only parameter. The callable will receive the item as it's parameter. If this parameter is typehinted, the manipulation will only be applied to items of that type.

Menu::new()
    ->add(Link::to('/', 'Home'))
    ->add(Html::raw('<a href="#" data-avatar>Profile</a>'))
    ->each(function (Link $link) {
        $link->addClass('link');
    })
    ->each(function (Html $html) {
        $html->addParentClass('html');
    });

In the above example, all links will receive a link class, and all html chunk parents (li's) will receive an html class.

each

Iterates over all existing items, and applies a manipulation.

Menu::new()
    ->add(Link::to('/foo-before', 'Foo before'))
    ->add(Link::to('/bar-before', 'Bar before'))
    ->each(function (Link $link) {

        // Return if string doesn't contain 'Foo'
        if (strpos($link->getText(), 'Foo') === false) {
            return;
        }

        $link->addClass('-has-foo');
    })
    ->add(Link::to('/foo-after', 'Foo after'))
    ->add(Link::to('/bar-after', 'Bar after'))
<ul>
    <li><a href="/foo-before" class="-has-foo">Foo before</a></li>
    <li><a href="/bar-before">Bar before</a></li>
    <li><a href="/foo-after">Foo after</a></li>
    <li><a href="/bar-after">Bar after</a></li>
</ul>

registerFilter

Registers a manipulation that will be applied on every new item.

Menu::new()
    ->add(Link::to('/foo-before', 'Foo before'))
    ->add(Link::to('/bar-before', 'Bar before'))
    ->registerFilter(function (Link $link) {

        // Return if string doesn't contain 'Foo'
        if (strpos($link->getText(), 'Foo') === false) {
            return;
        }

        $link->addClass('-has-foo');
    })
    ->add(Link::to('/foo-after', 'Foo after'))
    ->add(Link::to('/bar-after', 'Bar after'))
<ul>
    <li><a href="/foo-before">Foo before</a></li>
    <li><a href="/bar-before">Bar before</a></li>
    <li><a href="/foo-after" class="-has-foo">Foo after</a></li>
    <li><a href="/bar-after">Bar after</a></li>
</ul>

applyToAll

Applies a manipulation to all existing and future items no matter where it's called.

Menu::new()
    ->add(Link::to('/foo-before', 'Foo before'))
    ->add(Link::to('/bar-before', 'Bar before'))
    ->applyToAll(function (Link $link) {

        // Return if string doesn't contain 'Foo'
        if (strpos($link->getText(), 'Foo') === false) {
            return;
        }

        $link->addClass('-has-foo');
    })
    ->add(Link::to('/foo-after', 'Foo after'))
    ->add(Link::to('/bar-after', 'Bar after'))
<ul>
    <li><a href="/foo-before" class="-has-foo">Foo before</a></li>
    <li><a href="/bar-before">Bar before</a></li>
    <li><a href="/foo-after" class="-has-foo">Foo after</a></li>
    <li><a href="/bar-after">Bar after</a></li>
</ul>
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky