Manipulating Items | menu | Spatie

 SPATIE

  HTML Menu Generator
======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Menu](https://spatie.be/docs/menu/v3)  Items-in-depth  Manipulating Items

 Version   v3   v2   v1

 Other versions for crawler [v3](https://spatie.be/docs/menu/v3) [v2](https://spatie.be/docs/menu/v2) [v1](https://spatie.be/docs/menu/v1)

- [ Introduction ](https://spatie.be/docs/menu/v3/introduction)
- [ Postcardware ](https://spatie.be/docs/menu/v3/postcardware)
- [ Requirements ](https://spatie.be/docs/menu/v3/requirements)
- [ Installation and Setup ](https://spatie.be/docs/menu/v3/installation-and-setup)
- [ Questions &amp; Issues ](https://spatie.be/docs/menu/v3/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/menu/v3/changelog)
- [ About Us ](https://spatie.be/docs/menu/v3/about-us)

Basic usage
-----------

- [ Your First Menu ](https://spatie.be/docs/menu/v3/basic-usage/your-first-menu)
- [ Adding Items ](https://spatie.be/docs/menu/v3/basic-usage/adding-items)
- [ Building a menu from a data source ](https://spatie.be/docs/menu/v3/basic-usage/building-a-menu-from-a-data-source)

Items in depth
--------------

- [ Active Items ](https://spatie.be/docs/menu/v3/items-in-depth/active-items)
- [ Manipulating Items ](https://spatie.be/docs/menu/v3/items-in-depth/manipulating-items)
- [ Conditionally Adding Items ](https://spatie.be/docs/menu/v3/items-in-depth/conditionally-adding-items)

Controlling the html output
---------------------------

- [ Item Attributes ](https://spatie.be/docs/menu/v3/controlling-the-html-output/item-attributes)
- [ Parent Attributes ](https://spatie.be/docs/menu/v3/controlling-the-html-output/parent-attributes)
- [ Appending and Prepending Html ](https://spatie.be/docs/menu/v3/controlling-the-html-output/appending-and-prepending-html)
- [ Non-list Menus ](https://spatie.be/docs/menu/v3/controlling-the-html-output/non-list-menus)

Menus in your Laravel app
-------------------------

- [ Convenience Methods ](https://spatie.be/docs/menu/v3/menus-in-your-laravel-app/convenience-methods)
- [ Views as Menu Items ](https://spatie.be/docs/menu/v3/menus-in-your-laravel-app/views-as-menu-items)
- [ Conditional Items Based on Permissions ](https://spatie.be/docs/menu/v3/menus-in-your-laravel-app/conditional-items-based-on-permissions)
- [ Using Macros ](https://spatie.be/docs/menu/v3/menus-in-your-laravel-app/using-macros)

Examples
--------

- [ Bootstrap Menu ](https://spatie.be/docs/menu/v3/examples/bootstrap-menu)

 Manipulating Items
==================

###  On this page

1. [ Typehinting Callables ](#content-typehinting-callables)
2. [ each ](#content-each)
3. [ registerFilter ](#content-registerfilter)
4. [ applyToAll ](#content-applytoall)

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('Profile'))
    ->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'))
```

```

    Foo before
    Bar before
    Foo after
    Bar after

```

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'))
```

```

    Foo before
    Bar before
    Foo after
    Bar after

```

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'))
```

```

    Foo before
    Bar before
    Foo after
    Bar after

```
