DD and Dump Are coming to Collections in Laravel 5.5

Tutorials

June 27th, 2017

laravel-55-collections-dd.png

Coming to Laravel 5.5 is two brand new methods on the Collections class that will make debugging easier than ever before. These are dd and dump.

Pretend you have a simple collection setup and are piping it through a few filters:

1collect([1,2,3])->map(function($i){
2 return $i * 2;
3})->reject(function($i){
4 return $i < 3;
5});

Knowing what happens in each step of the chain is can be difficult and now you’ll have the option to either “dump” it out at a certain point, or “dump and die”. For example:

1collect([1,2,3])->map(function($i){
2 return $i * 2;
3})->dump()->reject(function($i){
4 return $i < 3;
5});

dump() outputs the results at that moment and then continues processing, here is the results when running that code:

1Collection {#181 ▼
2 #items: array:3 [▼
3 0 => 2
4 1 => 4
5 2 => 6
6 ]
7}

dd() on the other hand stops the process immediately and dumps out the results:

1collect([1,2,3])->map(function($i){
2 return $i * 2;
3})->dd()->reject(function($i){
4 return $i < 3;
5});

And the results:

1array:3 [▼
2 0 => 2
3 1 => 4
4 2 => 6
5]

These will be welcomed features in Laravel 5.5 and if you’d like to start using these today, Spatie released a 3rd party package named Collection Macros that includes both of these methods and a few additional helpers.

Filed in:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.