php - 如何在三元运算符中使用@break来打破laravel刀片文件中的foreach循环?
问题描述
我在 Laravel 做项目。我在刀片文件中有 foreach 循环我想在满足条件时打破 foreach 循环,但在这种情况下,我使用了三元运算符,在这个运算符中我想打破我的 foreach 循环。这是我的刀片文件,
@foreach($user->preference_attributes as $attributes)
<tr>
<th>{{ trans('labels.backend.access.users.tabs.content.overview.location') }}</th>
<td>{!!($attributes->name == 'location') ? $attributes->pivot->value @break : '' !!}</td>
</tr>
@endforeach
我已经这样做了,但它不起作用。我不知道如何用三元运算符打破这个。提前致谢。
解决方案
你不能做这个
{!!($attributes->name == 'location') ? $attributes->pivot->value @break : '' !!}
在您的错误情况下,blade
被转换为PHP
文件,这里有一个非常明显的语法错误,这就是为什么您必须使用PHP
语法{{ }}
或{!! !!}
这不是真正的原因,我收回这些话,只是因为PHP语法限制不能这样写
转换成 三元运算符的代码是函数的参数时,不可能实现在三元运算符中使用,考虑这个错误示例 你找到问题了吗?<?php echo e($attributes->name == 'location' ? $attributes->pivot->value : ''); ?>
e()
functionName( 'someStringParameter'; break; )
尝试这个
<tr>
<th>{{ trans('labels.backend.access.users.tabs.content.overview.location') }}</th>
<td>
@if($attributes->name == 'location')
{!! $attributes->pivot->value !!}
@break
@endif
</td>
</tr>
推荐阅读
- java - Android Studio 不断导入 import sun.jvm.hotspot.utilities.IntArray
- rust - 将向量中的每个元素与下一个元素进行比较
- c# - .net core 3.0 无法加载文件或程序集'System.Web.Http Version=5.2.7.0'
- python - 将需要修改的函数放在装饰器的什么位置
- php - 提供的 id 不存在 - php 中的 razorpay
- cors - .NET Core 3.0 中的 CORS
- javascript - 初学者问题:(可选)promise 结合箭头/访问结果/错误值
- pipe - Bazel 构建命令模棱两可的标准输出/标准错误,而管道
- r - 获取一个欠定系统的 n 个解的样本
- android - SD 卡保护 - 按键访问