laravel刀片文件中检查变量为null

时间:2018-12-11 06:22:27

标签: laravel laravel-5 laravel-5.3

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

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

但是没有运气。如何检查laravel 5.3刀片文件中是否设置了变量。

8 个答案:

答案 0 :(得分:2)

您只需执行{{ $material_details ?? 'second value' }}。阅读Elvis和Null合并运算符。

链接:

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

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

答案 1 :(得分:1)

您的代码仅在变量保留某个值时才打印数据。

尝试如下操作:

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

@else
 <tr>
   <td>null</td>
 </tr>

@endif

答案 2 :(得分:1)

尝试一下

@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

答案 3 :(得分:1)

您还可以使用'@isset()'秃头指令:

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

@empty($records)
    // $records is "empty"...
@endempty

答案 4 :(得分:0)

您可以通过使用如下所示的laravel三元运算符来实现

{!! !empty($material_details->pricing) ? '<tr><td>price is not null</td></tr>' : '<tr><td>Empty</td</tr>' !!}

答案 5 :(得分:0)

请尝试

@if($material_details->pricing != null)
   <tr>
     <td>price is not null</td>
   </tr>
@endif

答案 6 :(得分:0)

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

答案 7 :(得分:0)

您可以通过将Laravel isEmpty()链接到刀片服务器上的集合来简单地使用它。

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

有关Laravel API网站here的更多信息。