Laravel - Collections - Method Chunk
The chunk
method breaks the collection into multiple, smaller collections of a given size:
$collection = collect([1, 2, 3, 4, 5, 6, 7]);
$chunks = $collection->chunk(4);
$chunks->all();
// [[1, 2, 3, 4], [5, 6, 7]]
This method is especially useful in views when working with a grid system such as Bootstrap. For example, imagine you have a collection of Eloquent models you want to display in a grid:
@foreach ($products->chunk(3) as $chunk)
@foreach ($chunk as $product)
{{ $product->name }}
@endforeach
@endforeach