---
title: "Laravel's higher order collection proxies"
url: "https://aaronfrancis.com/writing/laravels-higher-order-collection-proxies"
path: "/writing/laravels-higher-order-collection-proxies"
type: technical_article
author: Aaron Francis
published: 2024-09-01
updated: 2026-11-10
topic: "Laravel & PHP"
description: Simplifying our codebase with a neat higher order collection proxy trick.
keywords: [higher-order collection proxies, laravel collections, eloquent, collection methods, magic properties, laravel dusk, browser automation, typehinting proxies, code readability, collection chaining, illuminate support]
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Laravel's higher order collection proxies

Higher-order collection proxies are a feature of Laravel that allows you to proxy methods to the underlying objects in a collection.

What the heck does that even mean?

Let's look at this simple example where we pull blocked users out of the database and send them an email informing them that they've been blocked.

```php
Users::where('blocked', 1)->get()->each(function(User $user) {
    $user->sendBlockedEmail();
});
```

This is a very simple implementation. We could clean it up by dropping the `get`, because `each` is implemented on the Eloquent Builder.

```php
Users::where('blocked', 1)->each(function(User $user) {
    $user->sendBlockedEmail();
});
```

However, `get` returns a Collection while the Builder's `each` does not. We want a Collection, so we'll restore the `get` and then call `each` on the Collection instead of the query builder.

```php
Users::where('blocked', 1)->get()->each(function(User $user) {
    $user->sendBlockedEmail();
});
```

This is where Higher Order Collection Proxies come in. Instead of calling a _method_ named `each`, we're just going to chain a _property_ named each. Like this:

```php
Users::where('blocked', 1)->get()->each;
```

This is a magic property. If we were to `dd` the result here, we'd get back an instance of `Illuminate\Support\HigherOrderCollectionProxy`. (You can see a list of all these magic proxy properties [here](https://github.com/illuminate/collections/blob/master/Traits/EnumeratesValues.php#L73).)

The magic here is that any method you call on the proxy will be proxied (!) to the underlying collection item. So revisiting our initial example, here's what it would look like now:

```php
Users::where('blocked', 1)->get()->each->sendBlockedEmail();
```

`sendBlockedEmail` is actually being called on each underlying `User` object. The neat part is that you can continue to chain collection methods on after that!

```php
Users::where('blocked', 1)->get()
    ->each->sendBlockedEmail()
    // Another collection method
    ->map(function(User $user) {
        // ...
    })
```

This can provide a super terse, readable collection chain. But! That's not all! 

## Advanced higher order collection proxies

Let's look at another example. This time using [Laravel Dusk's](https://laravel.com/docs/11.x/dusk) browser automation.

Browser automation is a powerful tool for testing web applications. It allows you to simulate user interactions with
your application, such as clicking buttons, filling out forms, and navigating between pages. Laravel Dusk is a browser
automation tool that makes it easy to write and run browser tests for your Laravel applications.

We'll take a look at how you can use Laravel's higher-order collection proxies to make your code more readable and
concise.

(Prefer to watch instead of read? Check out the video version of this article on
YouTube: [Poppin Off: Higher order collection proxies](https://www.youtube.com/watch?v=sjBS1Hk3iRE).)

In this example, suppose you are opening two different browsers in your test, doing the same thing in each browser to get it
set up, and then doing different things. You _could_ write this code like this:

```php
public function testExample(): void
{
    $this->browse(function (Browser $browser1, Browser $browser2) {
        $browser1->resize(624, 696);
        $browser1->move(12, 12);

        $browser2->resize(624, 696);
        $browser2->move(644, 12);

        $browser1->visit('https://reverb.laravel.com');
        $browser1->waitFor('figure');
        $browser1->scrollIntoView('figure');

        $browser2->visit('https://reverb.laravel.com');
        $browser2->waitFor('figure');
        $browser2->scrollIntoView('figure');

        Sleep::for(10)->seconds();
    }
}
```

In the example test we have two browsers, `$browser1` and `$browser2`, that we are setting up in the same way. We are
resizing and moving the browsers, visiting a URL, waiting for an element to appear, and scrolling to that element.

What I don't like here is that we're doing the same thing in each browser to prepare it.

Here's how we can make this a little more simplified:

Let's first collect `$browser1` and `$browser2` into a collection and save it to a variable called `$both`. Then we'll
do `dd($both)` to see what we have.

```php
public function testExample(): void
{
    $this->browse(function (Browser $browser1, Browser $browser2) {
        $both = collect([$browser1, $browser2]);

        dd($both);

        $browser1->resize(624, 696);
        $browser1->move(12, 12);

        $browser2->resize(624, 696);
        $browser2->move(644, 12);

        $browser1->visit('https://reverb.laravel.com');
        $browser1->waitFor('figure');
        $browser1->scrollIntoView('figure');

        $browser2->visit('https://reverb.laravel.com');
        $browser2->waitFor('figure');
        $browser2->scrollIntoView('figure');

        Sleep::for(10)->second();
    }
}
```

Now when we run the test, we have two browsers that open up. From the terminal output, we can see that `$both` is a
collection with two items in it, `$browser1` and `$browser2`.

What if we just added `each` to the end of the collect statement? And this is not an each callback `each()` but
just `each`.

```php
public function testExample(): void
{
    $this->browse(function (Browser $browser1, Browser $browser2) {
        $both = collect([$browser1, $browser2])->each;

        $dd($both);

        $browser1->resize(624, 696);
        $browser1->move(12, 12);

        $browser2->resize(624, 696);
        $browser2->move(644, 12);

        $browser1->visit('https://reverb.laravel.com');
        $browser1->waitFor('figure');
        $browser1->scrollIntoView('figure');

        $browser2->visit('https://reverb.laravel.com');
        $browser2->waitFor('figure');
        $browser2->scrollIntoView('figure');

        Sleep::for(10)->second();
    }
}
```

We run the test again and now we have an instance of `HigherOrderCollectionProxy`. And instead of using it right away, we just assign it to a variable!

We can call the duplicate actions on the `HigherOrderCollectionProxy`, `$both`, and it will apply the actions to each
browser in the collection.

```php
public function testExample(): void
{
    $this->browse(function (Browser $browser1, Browser $browser2) {
        $both = collect([$browser1, $browser2])->each;

        $browser1->resize(624, 696);
        $browser2->resize(624, 696);
        $both->resize(624, 696);

        // We still have access to the individual browsers!
        $browser1->move(12, 12);
        $browser2->move(644, 12);

        $browser1->visit('https://reverb.laravel.com');
        $browser2->visit('https://reverb.laravel.com');
        $both->visit('https://reverb.laravel.com');

        $browser1->waitFor('figure');
        $browser2->waitFor('figure');
        $both->waitFor('figure');

        $browser1->scrollIntoView('figure');
        $browser2->scrollIntoView('figure');
        $both->scrollIntoView('figure');

        Sleep::for(10)->second();
    }
}
```

All cleaned up, it's a lot shorter and more readable:

```php
public function testExample(): void
{
    $this->browse(function (Browser $browser1, Browser $browser2) {
        $both = collect([$browser1, $browser2])->each; 

        $both->resize(624, 696); 

        // We still have access to the individual browsers!
        $browser1->move(12, 12);
        $browser2->move(644, 12);

        $both->visit('https://reverb.laravel.com'); 
        $both->waitFor('figure'); 
        $both->scrollIntoView('figure'); 

        Sleep::for(10)->second();
    }
}
```

## Typehinting

If you don't like the fact that you lose the underlying type, you can typehint the Proxy:

```php
public function testExample(): void
{
    $this->browse(function (Browser $browser1, Browser $browser2) {
        /** @var \Laravel\Dusk\Browser $both */
        $both = collect([$browser1, $browser2])->each; 

        $both->resize(624, 696); 

        // We still have access to the individual browsers!
        $browser1->move(12, 12);
        $browser2->move(644, 12);

        $both->visit('https://reverb.laravel.com'); 
        $both->waitFor('figure'); 
        $both->scrollIntoView('figure'); 

        Sleep::for(10)->second();
    }
}
```

When we run the test, the `HigherOrderCollectionProxy` is going to proxy under to every item in the collection. So it
will run resize on all the items in the collection, for example.

We've simplified our code and made it more readable by using Laravel's higher-order collection proxies. It's also more maintainable, because if we want to change how we do something in both browsers, we only have to change it in one spot instead of two. Now imagine you've got 10 items in the collection and you'll see how valuable that is!

## YouTube video

Check out the video version of this article on YouTube!

> Watch on YouTube: https://www.youtube.com/watch?v=sjBS1Hk3iRE

---

## Related on aaronfrancis.com

- [Laravel tip: Higher order collection proxies (and Zevia)](/videos/laravel-tip-higher-order-collection-proxies-and-zevia) *(Video · 2024-08-09 · shared: higher-order collection proxies, laravel collections)*
- [Laravel Dusk: painless browser automation](/videos/laravel-dusk-painless-browser-automation) *(Video · 2024-07-19 · shared: laravel dusk, browser automation)*
- [Testing](/podcast/testing-699b73b6) *(Podcast · 2023-01-17 · shared: laravel dusk, browser automation)*
- [Testing my TUI](/videos/testing-my-tui) *(Video · 2025-02-06 · shared: laravel dusk)*
- [Reservable models in Laravel](/writing/reservable-models-in-laravel) *(Article · 2024-11-27 · shared: eloquent)*

## Relevant Resources from Aaron Francis

- **[Solo](https://soloterm.com)** (overview on site: [/software](/software)): All-in-one agentic development platform and terminal workspace built by Aaron Francis for orchestrating processes and AI coding agents.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `higher-order collection proxies` ([H-J](/llms/keywords/h-j.txt)), `laravel collections` ([K-L](/llms/keywords/k-l.txt)), `eloquent` ([D-E](/llms/keywords/d-e.txt)), `collection methods` ([C](/llms/keywords/c.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
