Tuesday, 28 November 2017

DD and Dump Collections in Laravel 5.5

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:
collect([1,2,3])->map(function($i){
    return $i * 2;
})->reject(function($i){
    return $i < 3;
});
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:
collect([1,2,3])->map(function($i){
    return $i * 2;
})->dump()->reject(function($i){
    return $i < 3;
});
dump() outputs the results at that moment and then continues processing, here is the results when running that code:
Collection {#181 ▼
  #items: array:3 [▼
    0 => 2
    1 => 4
    2 => 6
  ]
}
dd() on the other hand stops the process immediately and dumps out the results:
collect([1,2,3])->map(function($i){
    return $i * 2;
})->dd()->reject(function($i){
    return $i < 3;
});
And the results:
array:3 [▼
  0 => 2
  1 => 4
  2 => 6
]
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.

No comments:

Post a Comment

cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in Laravel

  WampServer: Download this file:  http://curl.haxx.se/ca/cacert.pem Place this file in the  C:\wamp64\bin\php\php7.1.9  folder Open  php.in...

Popular Articles