LoginSignup
6
5

More than 3 years have passed since last update.

Laravel $loop変数のプロパティと実行結果

Last updated at Posted at 2020-07-07

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>

実行結果

Screen Shot 0032-07-08 at 5.37.34.png

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5