How can I limit a @foreach ? I use a foreach to show the data of a related table and I would like to limit the results of that foreach I try to use this but it gives me an error
{{$count = 0;}} @foreach($product->post as $produc) {{if($count == 4) break; }}
///contenido
{{$count++;}}
any ideas ?
You can achieve this with @break :
But as an alternative you could use:
If
$count
it is always a number, I would propose that in the controller you divide that array of results into arrays of that size. Then you would pass the first array to the template.example:
Now, in $list you have an array of arrays. The first array has the first 4 elements, the second the second 4, and so on.
send $products to your template and iterate through the foreach.