Chunk by percentage

Submitted by jamosaur - 8 years ago

Chunk collections by percentages

// Add to a service provider

use Illuminate\Support\Collection;

Collection::macro('chunkpercent', function ($percentage) {
    $chunkNum = (int)ceil((count($this->items)*$percentage)/100);
    return collect($this->items)->chunk($chunkNum);
});