Skip to content

Make Collections a service that devs can extend #16773

Closed Answered by brandonkelly
webrgp asked this question in Ideas
Discussion options

You must be logged in to vote

Plugins can add new macros to collections using Illuminate\Support\Collection::macro(), as we are doing here:

// Register Collection::set() as an alias of put() - with support for bulk-setting values
Collection::macro('set', function(mixed $values) {
/** @var Collection $this */
if (is_array($values)) {
foreach ($values as $key => $value) {
$this->put($key, $value);
}
} else {
$this->put(...func_get_args());
}
return $this;
});
// Register Collection::one() as an alias of first(), for consistency with yii\db\Query.
Collection::macro('one', function

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@webrgp
Comment options

Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants