Created
September 5, 2016 05:20
-
-
Save sword-jin/5d77d10f980756dddde77977844cd781 to your computer and use it in GitHub Desktop.
collection macro chunkInto
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
Collection::macro('chunkInto', function ($slices) { | |
$slices = max(min($slices, $this->count()), 1); | |
if ($slices == 1) { | |
return collect([$this]); | |
} | |
$take = round($this->count() / $slices); | |
return collect([$this->take($take)]) | |
->merge($this->slice($take)->values()->chunkInto($slices - 1)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment