From Laravel Documentation , I changed this:
<ul>
@foreach($users as $user)
<li>{{ $loop->iteration }}</li>
<li>{{ $user->name }}</li>
<li>{{ $user->email }}</li>
@endforeach
</ul>
Into:
<ul>
@each('user.list', $users, 'user', 'user.empty')
</ul>
list.blade.php
<li>{{ $loop->iteration }}</li>
<li>{{ $user->name }}</li>
<li>{{ $user->email }}</li>
empty.blade.php
<li>There is no user is users.</li>
But, I get an error "Undefined variable: loop" in {{ $loop->iteration }}
So, is there any way to get iteration in @each?
As explained in the documentation :
Views rendered via
@each
do not inherit the variables from the parent view. If the child view requires these variables, you should use@foreach
and@include
instead.
So your code will not inherit the $loop
variable from the parent view, which is handling the looping.
Something like this might work:
@if (count($users)
@foreach ($users as $user)
@include("user.list", compact("user", "loop"))
@endforeach
@else
@include("user.empty")
@endif
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.