【问题标题】:Ternary operator syntax error in PHP LaravelPHP Laravel中的三元运算符语法错误
【发布时间】:2021-09-07 01:30:23
【问题描述】:

以下是我在刀片视图中遇到的语法错误:

语法错误,意外 '}',应为 ')'

代码

{{auth()->user()->role_id==4 ? '<a class="btn-floating mb-1 btn-flat waves-effect waves-light pink accent-2 white-text" href="/update-customer-installement/{{$list->id}}/{{$list->book_id}}"><i class="material-icons">delete</i></a>':'' }}

【问题讨论】:

  • 当您已经在双花括号中时,您不需要双花括号,因为您已经在刀片回显语法中。如果您在对象中下降超过 1 级,则只需要单个花括号
  • 使用 if 条件 @if(auth()->user()->role_id==4 ) 删除 @endif
  • 是的,对不起,我很抱歉,谢谢
  • 作为提示:如果您的代码像示例中的代码那样不可读,最好避免使用此类特殊运算符。对于您和其他开发人员来说,编写适当的循环更具可读性。此外,在“else”部分中没有任何内容的三元运算符是无用的

标签: php laravel laravel-blade


【解决方案1】:

你可以让它更简单:

@if(auth()->user()->role_id === 4)
    <a class="btn-floating mb-1 btn-flat waves-effect waves-light pink accent-2 white-text" href="/update-customer-installement/{{ $list->id }}/{{ $list->book_id }}">
        <i class="material-icons">delete</i>
    </a>
@endif

【讨论】:

  • 如果它有效,那么你可以接受这个答案。
【解决方案2】:

你可以用这个

{{ auth()->user()->role_id==4 ? '<a class="btn-floating mb-1 btn-flat waves-effect waves-light pink accent-2 white-text" href="/update-customer-installement/<?php echo $list->id;?>/<?php echo $list->book_id;?>"><i class="material-icons">delete</i></a>':'' }}

我测试过它 100% 有效 我希望你得到你的解决方案。

【讨论】:

    猜你喜欢
    • 2012-11-19
    • 2012-10-01
    • 2014-05-03
    • 2017-09-05
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    相关资源
    最近更新 更多