在 laravel 刀片文件中检查变量是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53718494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:12:25  来源:igfitidea点击:

checking variable is null in laravel blade file

laravellaravel-5laravel-5.3

提问by user3386779

I have the variable $material_details->pricing=nullI want to check the variable is set in laravel blade file.I have tried as

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

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

but no luck .How to check the variable is set or not in laravel 5.3 blade file.

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

回答by Singham

Try following code.

尝试以下代码。

@if(is_null($material_details))
    // whatever you need to do here
@else 

回答by usrNotFound

you can simply do {{ $material_details ?? 'second value' }}. Read on Elvis and Null coalescing operator.

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

Link:

关联:

Elvis:https://en.wikipedia.org/wiki/Elvis_operator

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

Null coalescing operator:https://en.wikipedia.org/wiki/Null_coalescing_operator

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

回答by dexter

Your code will only print data if the variable hold, some value.

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

Try something like following :

尝试类似以下内容:

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

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

@endif

回答by Jael

Try this

尝试这个

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

Or this

或这个

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

回答by Ayaz Shah

You can do it by using laravel ternary operator as like below

您可以通过使用 laravel 三元运算符来做到这一点,如下所示

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

回答by Shehara

Please try this

请试试这个

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

回答by GSangram

If are able to access your variable through {{ $material_details['pricing'] }} inside your blade file, 

then this will work to check and append value:

那么这将用于检查和附加值:

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

To check and append elements :

检查和追加元素:

@if($material_details['pricing'] != "null")
  <tr>
   <td>price is not null</td>
 </tr>
 @endif

回答by Abdelsalam Shahlol

You can simply use Laravel isEmpty()by chaining it to your collection on blade.

您可以isEmpty()通过将 Laravel链接到您在 Blade 上的收藏来简单地使用它。

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

More info on Laravel API website here.

有关 Laravel API 网站的更多信息,请访问此处