Skip to content

Instantly share code, notes, and snippets.

@sword-jin
Created September 5, 2016 05:20
Show Gist options
  • Save sword-jin/5d77d10f980756dddde77977844cd781 to your computer and use it in GitHub Desktop.
Save sword-jin/5d77d10f980756dddde77977844cd781 to your computer and use it in GitHub Desktop.
collection macro chunkInto
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