您可以使用chunk() https://laravel.com/docs/collections#method-chunk在集合上而不是take() https://laravel.com/docs/collections#method-take并传递每个块中您想要的项目数量
@foreach($alsoBought->chunk(3) as $three)
<div class="carousel-item @if ($loop->first) active @endif">
<div class="row">
@foreach($three as $bought)
<div class="col-4"><img class="w-100" src="{{ $bought['image'] }}" alt="First slide"></div>
@endforeach
</div>
</div>
@endforeach
From the docs https://laravel.com/docs/collections#method-chunk
The chunk
方法将集合分成多个给定大小的较小集合:
$collection = collect([1, 2, 3, 4, 5, 6, 7]);
$chunks = $collection->chunk(4);
$chunks->toArray();
// [[1, 2, 3, 4], [5, 6, 7]]