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:

dd

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

collect([ 1 , 2 , 3 ])->dd();

split

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(); $collection->first(); $collection->last();

validate

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

collect([ 'sebastian@spatie.be' , 'bla' ])->validate( 'email' ); collect([ 'sebastian@spatie.be' , 'freek@spatie.be' ])->validate( 'email' ); collect([ 'foo' , 'foo' ])->validate( function ($item) { return $item === 'foo' ; });

range

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();

ifAny

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

collect()->ifAny( function () { echo 'Hello' ; }); collect([ 1 , 2 , 3 ])->ifAny( function () { echo 'Hello' ; });

ifEmpty

This is the opposite of ifAny .

collect()->ifEmpty( function () { echo 'Hello' ; }); collect([ 1 , 2 , 3 ])->ifEmpty( function () { echo 'Hello' ; });

none

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' ); collect([ 'foo' ])->none( 'foo' ); collect([[ 'name' => 'foo' ]])->none( 'name' , 'bar' ); collect([[ 'name' => 'foo' ]])->none( 'name' , 'foo' ); collect([ 'name' => 'foo' ])->none( function ($key, $value) { return $key === 'name' && $value === 'bar' ; });

groupByModel

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.

<br />Article::all()->groupByModel( 'category' );

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.