【问题标题】:syntax for conditional operator with {{ }} in laravel bladelaravel 刀片中带有 {{ }} 的条件运算符的语法
【发布时间】:2020-12-25 02:33:58
【问题描述】:

我正在尝试对从控制器返回的值实现条件运算符以创建一些自定义视图。

front.blade

@if({{count($users)}} <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->
          <h3> total number of rows less than or equal to 5 </h3> 
@endif

控制器

$users = User::all();
return view('front', [ 'users'=>$users]);

错误是

语法错误,意外'

尝试了将条件放在 {{ }} 内或引用运算符或常量值的所有排列组合 5 错误仍然相同。我是 laravel 新手,这可能是 laravel 或 php 的根本错误。

【问题讨论】:

  • {{ }} 用于显示数据(它是一个回显)..刀片指令中的所有内容都是常规 PHP ...所以 @if (...) 中的所有内容都只是 PHP,您应该这样对待它.. . 玩得开心,祝你好运

标签: php laravel laravel-blade conditional-operator


【解决方案1】:

只需删除 {{}},除了 Blade 指令(在这种情况下为 @if)之外不需要它们

【讨论】:

    【解决方案2】:

    您需要删除 if 条件中的 {{ }}

    像这样。

      @if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->
         <h3> total number of rows less than or equal to 5 </h3> 
      @endif
    

    【讨论】:

      【解决方案3】:

      首先您需要了解何时需要使用花括号。
      当您在刀片文件中显示数据时,您需要使用花括号。喜欢

      Hello, {{ $name }}.
      

      您可以使用@if、@elseif、@else 和@endif 指令构造if 语句。这些指令的功能与它们的 PHP 对应指令相同:

      @if (count($records) === 1)
          I have one record!
      @elseif (count($records) > 1)
          I have multiple records!
      @else
          I don't have any records!
      @endif
      

      您的解决方案

       @if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->
           <h3> total number of rows less than or equal to 5 </h3> 
        @endif
      

      详情请查看 laravel 文档https://laravel.com/docs/7.x/blade#if-statements

      【讨论】:

        【解决方案4】:

        在 Controller 中像这样更改你的代码 -

        $users = User::all();
        return view('front', compact('users'));
        

        刀片文件代码-

        @if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 --><h3> total number of rows less than or equal to 5 </h3>@endif
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-17
          • 2022-09-22
          • 2013-08-07
          • 2016-04-03
          • 2018-11-04
          • 1970-01-01
          相关资源
          最近更新 更多