Laravel 5 Collections Static API: Adding Methods to the Higher Order Message List With proxy

1 min read

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

Signature

public static function proxy(
    $method
);

Example Use

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:

use Illuminate\Support\Collection;

Collection::proxy('contains');

Now, we could invoke the contains method on every object instance in a collection of objects like so:

$userCollection->contains->isAdmin();

Internally, the previous code example is executed like so:

$userCollection->contains(function ($user) {
   return $user->isAdmin(); 
});

Comments

There are no comments. Be the first to comment!

Up next