What's New in Laravel 5.2 Collections

Tag: Laravel v5.2+

Confession time - I wrote a "book". Yes, me too. Actually, I call it an "ePamphlet", and it is only about 8-10 pages, completely FREE, and written very narrowly about the Laravel Collection class. Laravel Collections Unraveled is just a deep dive into the class that I thought was too long for a blog post, and could be best presented via a LeanPub eBook. We'll dig into the source code of this popular class and see some use cases for the major functions.

Laravel 5.2 is coming out in December and there are a lot of nice new features to be excited about. Collections are no exception - there are several new functions I think you will really appreciate.

The first two are "borrowed" from the Request and Eloquent Collection objects. I say "borrowed" in quotes because really these two are based on the `Arr` class and have just been given wrapper functions on the Support\Collection. They are mirror images of each other:

`except()` Ex. - $arr = Collection(['name' => 'Jeff', 'twitter' => 'codebyjeff']); dump( $arr->except('name') ); // result: array{ 'twitter' => 'codebyjeff' } `only()` dump( $arr->only('twitter') ); // result: array{ 'twitter' => 'codebyjeff' }

A small change has been made to `prepend()` so that you can give it a key as well as a value, such as (taken from the tests, which is an excellent way to study these!):

$c = new Collection(['one' => 1, 'two' => 2]); $this->assertEquals(['zero' => 0, 'one' => 1, 'two' => 2], $c->prepend(0, 'zero')->all());

Just remember it is $value, $key and not the other way around like you'd think with an array.

Function `flatMap()` combines mapping with the `flatten()` function, allowing you to write essentially a custom callback to retrieve nested arrays, then collapse them into a single dimensional array. This might be used, for example, to look through all of the users' choices and return a list of them.

$data = new Collection([ ['name' => 'taylor', 'hobbies' => ['programming', 'basketball']], ['name' => 'adam', 'hobbies' => ['music', 'powerlifting']], ]); // this callback simply returns the 'hobbies', but could do anything you might do in an `array_map` $data = $data->flatMap( function ($person) { return $person['hobbies']; } ); $this->assertEquals(['programming', 'basketball', 'music', 'powerlifting'], $data->all());

Finally, one of the simplest yet potentially most interesting is `macroable()`. Simple, because it only requires the addition of the `Trait Macroable`; potentially most useful because it allows you to create your own Collection-level scopes, putting together any chained collection functions along with any other business logic you may wish to add.

// Foo() macro : unique values starting with A Collection::macro('foo', function () { return $this->filter(function ($item) { return strpos($item, 'a') === 0; }) ->unique() ->values(); }); $c = new Collection(['a', 'a', 'aa', 'aaa', 'bar']); $this->assertSame(['a', 'aa', 'aaa'], $c->foo()->all());

When I read this, the first thing that came to mind was stored procedures or business intelligence objects to encapsulate business logic for the other developers to make use of. I'm interested in seeing examples of how people are using it - go ahead and grab a copy of 5.2 dev and let me know your ideas!