foeach
の$loop変数のプロパティ
$loop ループ変数 | プロパティ |
---|---|
index | インデックス番号 |
iteration | 繰り返し数(1スタート) |
remaining | 残っている要素数 |
count | 配列の総数 |
first | 最初の項目か |
last | 最後の項目か |
even | 偶数回目か |
odd | 奇数回目か |
depth | 入れ子レベル |
parent | 親のループ変数(ネストの入れ子の場合) |
サンプル
Controller
ViewController.php
public function foreach_loop()
{
return view('view.foreach_loop', [
'weeks' => ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
]);
}
bladeテンプレート
foreach_loop.blade.php
<table class="table">
<tr>
<th>value</th>
<th>index</th>
<th>iteration</th>
<th>count</th>
<th>first</th>
<th>last</th>
<th>even</th>
<th>odd</th>
<th>depth</th>
</tr>
@foreach($weeks as $week)
<tr>
<td>{{ $week }}</td>
<td>{{ $loop->index }}</td>
<td>{{ $loop->iteration }}</td>
<td>{{ $loop->count }}</td>
<td>{{ $loop->first }}</td>
<td>{{ $loop->last }}</td>
<td>{{ $loop->even }}</td>
<td>{{ $loop->odd }}</td>
<td>{{ $loop->depth }}</td>
</tr>
@endforeach
</table>