Reputation: 371
I would like to know what to do in Blade to have the equivalent of this code. I need to do an iteration inside a foreach. I see the blade loop variable like $loop->index
or $loop->remaining
but I need to know how to use it to make the equivalent of the code below.
<?php
for ($i = 0; $i < 3; $i++) {
$result[$i]['id'];
$result[$i]['name'];
$result[$i]['email'];
}
Upvotes: 5
Views: 26328
Reputation: 371
Thank mhrabiee. i found the solution.
@foreach($things as $thing)
@if( $loop->first or $loop->iteration <= 3 )
<tr>
<td>{{$thing)->id}}</td>
<td>{{$thing)->name}}</td>
<td>{{$thing)->email}}</td>
</tr>
@endif
@endforeach
this start first iteration
$loop->first
and this stop iteration after 3 loop
$loop->iteration <= 3
and voila !
Upvotes: 8
Reputation: 815
Your question is a bit vague but exact equivalent of your code is something like this:
@for ($i = 0; $i < 3; $i++)
{{ $result[$i]['id'] }}
{{ $result[$i]['name'] }}
{{ $result[$i]['email'] }}
@endfor
By the way if you want to iterate through something like $results
array you can do this:
@foreach ($results as $result)
<div>{{ $result->id }}</div>
<div>{{ $result->name }}</div>
<div>{{ $result->email }}</div>
@endforeach
PS: You can learn more about for loops in Laravel's Blade Documentation.
Upvotes: 4