Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Some handy collection macros

Laravel’s collection class is truly wonderful. It contains a lot of handy methods and you can create some very elegant code with it. In client projects I found myself adding the same macro’s over and over again. That’s why my colleague Seb and I took some time to create a package aptly called laravel-collection-macros that contains those macros. You can view the source code and installation instructions on GitHub.

To install the package you’ll only need to pull it via composer and register the service provider. With that out of the way the collection class in your project will gain these methods:


This macro dumps the contents of the collection and terminates the script. It makes debugging a collection much easier.



Image you need to render some items in a given amount of columns. With the split macro you can split the 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';


With this macro you can validate all items inside a collection. You can pass a validate rule to it.

collect([[email protected]', 'bla'])->validate('email'); // returns false
collect([[email protected]', [email protected]'])->validate('email'); // returns true

//for fine grained control you may also pass a closure.
collect(['foo', 'foo'])->validate(function ($item) {
   return $item === 'foo';
}); // 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]


The passed callable will only be called if the collection isn’t empty. The entire collection will be returned.

collect()->ifAny(function() { // empty collection so this won't get called
   echo 'Hello';

collect([1, 2, 3])->ifAny(function() { // non-empty collection so this will get called
   echo 'Hello';


This is the opposite of ifAny.

collect()->ifEmpty(function() { // empty collection so this will called
   echo 'Hello';

collect([1, 2, 3])->ifEmpty(function() { // 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


This is a very opinionated macro. It works similar to groupBy, but it’ll group the collection by an Eloquent model instead of a string. Imagine that your project contains an Article model that has a relationship with a Category model. In your view you want to render all articles grouped by category.

Sure you could to this:

Article::all()->groupBy(function(Article $article) {
  return $article->category->name;

That’ll work but the downside of it is that you only have the name of the category left when looping over the resulting collection.

When using the groupByModel macro you still can get to all properties of the category.

Article::all()->groupByModel('category'); //this returns something like this // [ // [ // 'category' => $category, // 'items' => [ // [ // articles models with the category], // ], // ], // [ // 'category' => $category2, // 'items' => [ // [ // article models with $category 2 ], // ], // ], // ];

Want to know more about the package, then head over to GitHub to learn all the options. We do accept new macro’s to the package, so if you’ve got a macro lying around that could be useful to your fellow artisans, send us a PR.

You can take a look at the Laravel packages we previously made on our website. If one of our packages makes it to your production environment don’t forget to send us a postcard.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.