$loop->firstは最初の繰り返しかどうかをみると書いてあったのですが、
@foreachで$data = ['one', 'two', 'three', 'four', 'five'];の'one'が呼び出されたときに実行されるということですか?
もしそうであれば'one'は@if ($loop->first)を通り、@endifには'one'の値がよびだされず以下のような出力になると思いました。($loop->lastも)
※データ一覧
No,2. two
No,3. three
No,4. four
--ここまで
しかし、出力結果はこうでした。
※データ一覧
No,1. one
No,2. two
No,3. three
No,4. four
No,5. five
--ここまで
これは、'one'が呼び出される前に@if ($loop->first)が通り、'five'が通ったあとに@if ($loop->last)が通っている認識でいいのでしょうか?
php
1// resources/views/hello/index.blade.php 2 3<html> 4<head> 5 <title>Hello/Index</title> 6</head> 7<body> 8 <h1>Blade/Index</h1> 9 <p>@forディレクティブの例</p> 10 @foreach ($data as $item) 11 @if ($loop->first) 12 <p>※データ一覧</p><ul> 13 @endif 14 <li>No,{{$loop->iteration}}. {{$item}}</li> 15 @if ($loop->last) 16 </ul><p>--ここまで</p> 17 @endif 18 @endforeach 19</body> 20</html>
php
1// app/Http/Controllers/HelloController.php 2 3<?php 4 5namespace App\Http\Controllers; 6 7use Illuminate\Http\Request; 8use Illuminate\Http\Response; 9 10class HelloController extends Controller 11{ 12 public function index() 13 { 14 $data = ['one', 'two', 'three', 'four', 'five']; 15 return view('hello.index', ['data'=>$data]); 16 } 17}
php
1// routes/web.php 2 3<?php 4 5Route::get('hello', 'HelloController@index');
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/06/14 08:10