checking variable is null in laravel blade file

72.1k Views Asked by At

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

@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.

9

There are 9 best solutions below

0
oreopot On

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
1
Ayaz Ali Shah On

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

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

Please try this

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

Try following code.

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

You can use either Null Coalescing or Elvis Operator

Null Coalescing Operator

{{ $material_details ?? 'second value' }} //object
{{ $material_details->property ?? 'second value' }} //Object Property

Elvis Operator (checks given value is empty or null)

{{ $material_details ?: 'default value' }} //Object
{{ $material_details->property ?: 'default value' }} //Object Property

Read on Elvis and Null coalescing operator.

Links:

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

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

0
GSangram On
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
0
Abdelsalam Shahlol On

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

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

More info on Laravel API website here.

0
Jael On

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
0
julian On

You can also use '@isset()' balde directive :

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

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