如何在三元操作中使用@break来破坏laravel刀片文件中的foreach循环?

时间:2018-04-27 05:49:53

标签: php laravel

我在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

我这样做但是没有用。我没有得到如何用三元运算符打破这个。提前谢谢。

3 个答案:

答案 0 :(得分:1)

你不能这样做

{!!($attributes->name == 'location') ? $attributes->pivot->value @break : '' !!}

在您的错误情况下,blade转换为PHP文件,此处存在非常明显的语法错误,这就是您必须在{{1}中使用PHP语法的原因}或{{ }}

enter image description here

这不是真正的原因,我收回这些话,只是因为PHP语法限制不能这样写

  

无法在三元运算符中使用
  当代码转换为   {!! !!}   三元运算符是函数<?php echo e($attributes->name == 'location' ? $attributes->pivot->value : ''); ?>的参数,请考虑此错误示例   e()   你发现了这个问题吗?

试试这个

functionName( 'someStringParameter'; break; )

答案 1 :(得分:0)

您无法使用if方法?

@if($attributes->name == 'location')
{{ $attributes->pivot->value}} @break
@endif

答案 2 :(得分:0)

试试这个不确定

{{ $attributes->name == 'location' ? $attributes->pivot->value @break : '' }}
相关问题