代码之家  ›  专栏  ›  技术社区  ›  user3386779

检查laravel blade文件中的变量是否为空

  •  0
  • user3386779  · 技术社区  · 6 年前

    我有变量 $material_details->pricing=null 我想检查变量是否设置在laravel刀片文件中

    @isset($material_details->pricing)
     <tr>
       <td>price is not null</td>
     </tr>
    @endisset
    

    5 回复  |  直到 6 年前
        1
  •  7
  •   usrNotFound    6 年前

    你可以简单地做 {{ $material_details ?? 'second value' }} . 读取Elvis和Null合并运算符。

    链接:

    猫王: https://en.wikipedia.org/wiki/Elvis_operator

    空合并运算符: https://en.wikipedia.org/wiki/Null_coalescing_operator

        2
  •  7
  •   Karl Hill    6 年前

    试试下面的代码。

    @if(is_null($material_details))
        // whatever you need to do here
    @else 
    
        3
  •  1
  •   oreopot    6 年前

    您的代码只会在变量保持某个值时打印数据。

    尝试以下方法:

    @if(isset($material_details->pricing))
     <tr>
       <td>price is not null</td>
     </tr>
    
    @else
     <tr>
       <td>null</td>
     </tr>
    
    @endif
    
        4
  •  1
  •   Jael    5 年前

    试试这个

    @if(isset($material_details->pricing))
     <tr>
       <td>NOT NULL</td>
     </tr>
    @else
     <tr>
       <td>NULL</td>
     </tr>
    @endif
    

    @if(empty($material_details->pricing))
     <tr>
       <td>NULL</td>
     </tr>
    @else
     <tr>
       <td>NOT NULL</td>
     </tr>
    @endif
    
        5
  •  0
  •   Ayaz Ali Shah    6 年前

    您可以使用如下所示的laravel三元运算符

    {!! !empty($material_details->pricing) ? '<tr><td>price is not null</td></tr>' : '<tr><td>Empty</td</tr>' !!}
    
        6
  •  0
  •   Shehara    6 年前

    请试试这个

    @if($material_details->pricing != null)
       <tr>
         <td>price is not null</td>
       </tr>
    @endif
    
        7
  •  0
  •   GSangram    6 年前
    If are able to access your variable through {{ $material_details['pricing'] }} inside your blade file, 
    

    然后检查并附加值:

    {{ ($material_details['pricing'] == "null") ? "null value" : $material_details['pricing'] }}
    

    要检查和附加元素:

    @if($material_details['pricing'] != "null")
      <tr>
       <td>price is not null</td>
     </tr>
     @endif
    
        8
  •  0
  •   Abdelsalam Shahlol Rohit Martires    5 年前

    你可以简单地使用拉威尔 isEmpty() 用链子把你的收藏品绑起来。

    @if($data->quotes->isEmpty()) 
      //Empty
    @else
    // Not Empty
    @endif
    

    更多信息请登录Laravel API网站 here .

        9
  •  0
  •   julian    4 年前

    @isset($records)
        // $records is defined and is not null...
    @endisset
    
    @empty($records)
        // $records is "empty"...
    @endempty