Chunk by percentage

Submitted by jamosaur - 1 year 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);
});
comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks