[英]How to break a foreach loop in laravel blade view?
I have a loop like this:我有一个这样的循环:
@foreach($data as $d)
@if(condition==true)
{{$d}}
// Here I want to break the loop in above condition true.
@endif
@endforeach
From the Blade docs :来自Blade 文档:
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
you can break like this你可以这样休息
@foreach($data as $d)
@if($d=="something")
{{$d}}
@if(codition)
@break
@endif
@endif
@endforeach
Basic usage基本用法
By default, blade doesn't have @break
and @continue
which are useful to have.默认情况下,blade 没有有用的@break
和@continue
。 So that's included.所以这包括在内。
Furthermore, the $loop
variable is introduced inside loops, (almost) exactly like Twig.此外, $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
What is the difference between Blade @break and @continue directives?? Blade @break 和 @continue 指令有什么区别?
Official docs say: When using loops you may also end the loop or skip the current iteration using the @continue<\/strong> and @break<\/strong> directives:<\/em>官方文档说:使用循环时,您还可以使用@continue<\/strong>和@break<\/strong>指令结束循环或跳过当前迭代:<\/em>
@foreach ($users as $user)
@if ($user->type == 1)
@continue
@endif
<li>{{ $user->name }}</li>
@if ($user->number == 5)
@break
@endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.