【问题标题】:Laravel: Multiple if conditions in single line in Laravel bladeLaravel:Laravel刀片中单行的多个if条件
【发布时间】:2017-11-17 15:02:04
【问题描述】:

在网络应用程序中,我有一个按 ASC/DESC 顺序对数据进行排序的链接。我希望我的链接首先处于默认状态,第一次单击时它将按 ASC 顺序对数据进行排序,如果再次单击它将使其成为 DESC 顺序。

http//localhost/master/branch/ - 默认网址

http//localhost/master/branch/?sort=name&dir=asc - 用户第一次点击链接

http//localhost/master/branch/?sort=name&dir=desc - 再次点击相同的链接

我需要根据默认的/asc/desc 命令修改链接的图标。我在锚标记中添加了以下条件,但这看起来不起作用:

<a href="{{ route('branch.index') }}?sort=name&dir={{ Request::input('dir') == 'asc' ? 'desc' : 'asc' }}">Name</a> <i class="fa fa-{{ !Request::input('dir') ? 'sort' : '(Request::input('dir') == 'asc' ? sort-alpha-asc' : 'sort-alpha-desc'.' }}"></i>

但上面给出了FatalErrorException 并说syntax error, unexpected 'dir' (T_STRING)

请告诉我如何在一行中添加多个条件?

【问题讨论】:

    标签: php html laravel laravel-5.4


    【解决方案1】:

    您的代码中有多余的'.,或者您可以将它们转义。你可以试试这个,

    <a href="{{ route('branch.index') }}?sort=name&dir={{ Request::input('dir') == 'asc' ? 'desc' : 'asc' }}">Name</a>
    <i class="fa fa-{{ !Request::input('dir') ? 'sort' : (Request::input('dir') == 'asc' ? 'sort-alpha-asc' : 'sort-alpha-desc') }}"></i>
    

    【讨论】:

    • 看起来很棒并且工作正常。完美的。但是在您的代码中,我们需要 ' 之前 sort-alpha-asc 其他一切都很好。我已经编辑了你的答案,请检查。感谢您的帮助。
    猜你喜欢
    • 2017-11-14
    • 2017-09-12
    • 2017-04-10
    • 2021-10-08
    • 2014-01-17
    • 2019-08-14
    • 1970-01-01
    • 2015-09-19
    • 2022-09-22
    相关资源
    最近更新 更多