Manipulating Items | menu | Spatie

 SPATIE

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Menu](https://spatie.be/docs/menu/v1)  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)

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

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

- [ Your First Menu ](https://spatie.be/docs/menu/v1/basic-usage/your-first-menu)
- [ Adding Items ](https://spatie.be/docs/menu/v1/basic-usage/adding-items)

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

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

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

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

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

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

Examples
--------

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

API
---

- [ Menu ](https://spatie.be/docs/menu/v1/api/menu)
- [ Link ](https://spatie.be/docs/menu/v1/api/link)
- [ Html ](https://spatie.be/docs/menu/v1/api/html)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/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 recieve 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

```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
