Last active
December 15, 2018 20:41
-
-
Save ryanscherler/f7e76fa5d8e84a51068eb97c754acbff to your computer and use it in GitHub Desktop.
Extends Laravel collections to convert array items to a collection recursively.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Support\Collection; | |
use \Illuminate\Support\Collection; | |
class CustomCollection extends Collection { | |
/** | |
* Converts array items to a collections recursively. | |
* Usage: $collection = Collection::make($array)->collectArrayItems(); | |
* | |
* @return mixed | |
*/ | |
public function collectArrayItems() | |
{ | |
$this->each(function($item, $key) { | |
if (is_countable($item)) { | |
$collection = self::make($item)->collectArrayItems(); | |
$this->put($key, $collection); | |
} | |
}); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment