如何在 laravel blade 视图中打破 foreach 循环?
How to break a foreach loop in laravel blade view?
我有一个这样的循环:
@foreach($data as $d)
@if(condition==true)
{{$d}}
// Here I want to break the loop in above condition true.
@endif
@endforeach
如果条件满足,我想在数据显示后打破循环。
如何在 laravel blade 视图中实现?
来自Blade docs:
When using loops you may also end the loop or skip the current
iteration:
@foreach ($users as $user)
@if ($user->type == 1)
@continue
@endif
<li>{{ $user->name }}</li>
@if ($user->number == 5)
@break
@endif
@endforeach
你可以这样破解
@foreach($data as $d)
@if($d === "something")
{{$d}}
@if(condition)
@break
@endif
@endif
@endforeach
Basic usage
默认情况下,blade 没有 @break
和 @continue
这两个有用的东西。所以包括在内。
此外,$loop
变量是在循环中引入的,(几乎)与 Twig 完全一样。
Basic Example
@foreach($stuff as $key => $val)
$loop->index; // int, zero based
$loop->index1; // int, starts at 1
$loop->revindex; // int
$loop->revindex1; // int
$loop->first; // bool
$loop->last; // bool
$loop->even; // bool
$loop->odd; // bool
$loop->length; // int
@foreach($other as $name => $age)
$loop->parent->odd;
@foreach($friends as $foo => $bar)
$loop->parent->index;
$loop->parent->parentLoop->index;
@endforeach
@endforeach
@break
@continue
@endforeach
@foreach($data as $d)
@if(condition==true)
{{$d}}
@break // Put this here
@endif
@endforeach
官方文档说:当使用循环时,您也可以使用 @continue 和 @break 结束循环或跳过当前迭代 指令:
@foreach ($users as $user)
@if ($user->type == 1)
@continue
@endif
<li>{{ $user->name }}</li>
@if ($user->number == 5)
@break
@endif
@endforeach
我有一个这样的循环:
@foreach($data as $d)
@if(condition==true)
{{$d}}
// Here I want to break the loop in above condition true.
@endif
@endforeach
如果条件满足,我想在数据显示后打破循环。
如何在 laravel blade 视图中实现?
来自Blade docs:
When using loops you may also end the loop or skip the current iteration:
@foreach ($users as $user)
@if ($user->type == 1)
@continue
@endif
<li>{{ $user->name }}</li>
@if ($user->number == 5)
@break
@endif
@endforeach
你可以这样破解
@foreach($data as $d)
@if($d === "something")
{{$d}}
@if(condition)
@break
@endif
@endif
@endforeach
Basic usage
默认情况下,blade 没有 @break
和 @continue
这两个有用的东西。所以包括在内。
此外,$loop
变量是在循环中引入的,(几乎)与 Twig 完全一样。
Basic Example
@foreach($stuff as $key => $val)
$loop->index; // int, zero based
$loop->index1; // int, starts at 1
$loop->revindex; // int
$loop->revindex1; // int
$loop->first; // bool
$loop->last; // bool
$loop->even; // bool
$loop->odd; // bool
$loop->length; // int
@foreach($other as $name => $age)
$loop->parent->odd;
@foreach($friends as $foo => $bar)
$loop->parent->index;
$loop->parent->parentLoop->index;
@endforeach
@endforeach
@break
@continue
@endforeach
@foreach($data as $d)
@if(condition==true)
{{$d}}
@break // Put this here
@endif
@endforeach
官方文档说:当使用循环时,您也可以使用 @continue 和 @break 结束循环或跳过当前迭代 指令:
@foreach ($users as $user)
@if ($user->type == 1)
@continue
@endif
<li>{{ $user->name }}</li>
@if ($user->number == 5)
@break
@endif
@endforeach