April 22, 2018 —John Koster
The proxy
static method is used to add additional methods to the list of methods that should be proxied for use with higher order messaging. These proxied methods allow for a cleaner syntax when invoking methods on collections of objects.
By default, the following methods are available for higher order messaging:
Method | Method | Method | Method |
---|---|---|---|
average | avg | contains | each |
every | filter | first | flatMap |
keyBy | map | partition | reject |
sortBy | sortByDesc | sum |
1public static function proxy(2 $method3);
The following example demonstrates how to invoke the proxy
static method. Let's imagine that we did not have the contains
method in the default list of proxied methods, we could add it to the list like so:
1use Illuminate\Support\Collection;2 3Collection::proxy('contains');
Now, we could invoke the contains
method on every object instance in a collection of objects like so:
1$userCollection->contains->isAdmin();
Internally, the previous code example is executed like so:
1$userCollection->contains(function ($user) {2 return $user->isAdmin();3});
∎
The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.