This repository contains some useful macro collections.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
The best postcards will get published on the open source page on our website.
You can pull in the package via composer:
composer require spatie/laravel-collection-macros
This service provider must be installed.
// config/app.php
'providers' => [
...
Spatie\CollectionMacros\CollectionMacroServiceProvider::class,
];
These macro's will be added to the Illuminate\Support\Collection
class.
Dumps the contents of the collection and terminates the script. This macro makes debugging a collection much easier.
collect([1,2,3])->dd();
Similar to groupBy
, but groups the collection by an Eloquent model. Since the key is an object instead of an integer or string, the results are divided into separate arrays.
$collection = collect([
['model' => $model1, 'foo' => 'bar'],
['model' => $model1, 'foo' => 'baz'],
['model' => $model2, 'foo' => 'qux'],
]);
$collection->groupByModel('model');
// [
// [
// 'model' => $model1,
// 'items' => [
// ['model' => $model1, 'foo' => 'bar'],
// ['model' => $model1, 'foo' => 'baz'],
// ],
// ],
// [
// 'model' => $model2,
// 'items' => [
// ['model' => $model2, 'foo' => 'qux'],
// ],
// ],
// ];
You can also use a callable for more flexibility:
$collection->groupByModel(function ($item) {
return $item['model']
});
If you want to specify the model key's name, you can pass it as the second parameter:
$collection->groupByModel('model', 'myModel');
// [
// [
// 'myModel' => $model1,
// 'items' => [
// ['model' => $model1, 'foo' => 'bar'],
// ['model' => $model1, 'foo' => 'baz'],
// ],
// ],
// [
// 'myModel' => $model2,
// 'items' => [
// ['model' => $model2, 'foo' => 'qux'],
// ],
// ],
// ];
Executes the passed callable if the collection isn't empty. The entire collection will be returned.
collect()->ifAny(function(Collection $collection) { // empty collection so this won't get called
echo 'Hello';
});
collect([1, 2, 3])->ifAny(function(Collection $collection) { // non-empty collection so this will get called
echo 'Hello';
});
Executes the passed callable if the collection is empty. The entire collection will be returned.
collect()->ifEmpty(function(Collection $collection) { // empty collection so this will called
echo 'Hello';
});
collect([1, 2, 3])->ifEmpty(function(Collection $collection) { // non-empty collection so this won't get called
echo 'Hello';
});
Checks whether a collection doesn't contain any occurrences of a given item, key-value pair, or passing truth test. The function accepts the same parameters as the contains
collection method.
collect(['foo'])->none('bar'); // returns true
collect(['foo'])->none('foo'); // returns false
collect([['name' => 'foo']])->none('name', 'bar'); // returns true
collect([['name' => 'foo']])->none('name', 'foo'); // returns false
collect(['name' => 'foo'])->none(function ($key, $value) {
return $key === 'name' && $value === 'bar';
}); // returns true
Creates a new collection instance with a range of numbers. This functions accepts the same parameters as PHP's standard range
function.
collect()->range(1, 3)->toArray(); //returns [1,2,3]
Splits a collection into a the given number of groups.
$collection = collect(['a', 'b', 'c', 'd', 'e', 'f'])->split(3);
$collection->count(); // returns 3
$collection->first(); // returns a collection with 'a' and 'b';
$collection->last(); // returns a collection with 'e' and 'f';
Returns true
if the given $callback
returns true for every item. If $callback
is a string or an array, regard it as a validation rule.
collect(['foo', 'foo'])->validate(function ($item) {
return $item === 'foo';
}); // returns true
collect(['[email protected]', 'bla'])->validate('email'); // returns false
collect(['[email protected]', '[email protected]'])->validate('email'); // returns true
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
The MIT License (MIT). Please see License File for more information.