Skip to content

[5.5] Document Collection::macro() method #3700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- [Introduction](#introduction)
- [Creating Collections](#creating-collections)
- [Extending Collections](#extending-collections)
- [Available Methods](#available-methods)
- [Higher Order Messages](#higher-order-messages)

Expand Down Expand Up @@ -29,6 +30,25 @@ As mentioned above, the `collect` helper returns a new `Illuminate\Support\Colle

> {tip} The results of [Eloquent](/docs/{{version}}/eloquent) queries are always returned as `Collection` instances.

<a name="extending-collections"></a>
### Extending Collections

Collections are macroable, meaning that you can add new methods to the `Collection` class at run time without being required to extend it through inheritance.

For example, here's how we could define a new method on the `Collection` called `mean` that is only an alias to the `average` method:

Collection::macro('mean', function ($callback = null) {
return $this->average($callback);
});

$collection = collect([1, 2, 3]);

$mean = $collection->mean();

// 2

Typically, you should declare your collection's macros in a [service provider](/docs/{{version}}/providers).

<a name="available-methods"></a>
## Available Methods

Expand Down Expand Up @@ -80,6 +100,7 @@ For the remainder of this documentation, we'll discuss each method available on
[keyBy](#method-keyby)
[keys](#method-keys)
[last](#method-last)
[macro](#method-macro)
[map](#method-map)
[mapWithKeys](#method-mapwithkeys)
[max](#method-max)
Expand Down Expand Up @@ -739,6 +760,11 @@ You may also call the `last` method with no arguments to get the last element in

// 4

<a name="method-macro"></a>
#### `macro()` {#collection-method}

The static `macro` method allows you to add methods to the `Collection` class at run time. Refer to the [Extending Collections](#extending-collections) section for more information.

<a name="method-map"></a>
#### `map()` {#collection-method}

Expand Down