I know it's been a while, but i found this answer on laracast and it seems to solve the issue better, cause it make it recursive.
This solution I've got from https://gist.github.com/brunogaspar/154fb2f99a7f83003ef35fd4b5655935 github and works like a charm.
\Illuminate\Support\Collection::macro('recursive', function () {
return $this->map(function ($value) {
if (is_array($value) || is_object($value)) {
return collect($value)->recursive();
}
return $value;
});
});
than you go like:
$data = [
[
'name' => 'John Doe',
'emails' => [
'john@doe.com',
'john.doe@example.com',
],
'contacts' => [
[
'name' => 'Richard Tea',
'emails' => [
'richard.tea@example.com',
],
],
[
'name' => 'Fergus Douchebag', // Ya, this was randomly generated for me :)
'emails' => [
'fergus@douchebag.com',
],
],
],
],
];
$collection = collect($data)->recursive();