You know that we can check whether a variable is set or not in laravel using some blade directives like @isset, @if, and @empty, etc. In this tutorial, I will show you how to check whether a variable exists or not in Laravel. You can use this code in laravel 6, laravel 7, laravel 8, and laravel 9 versions.
Sometimes we need to check blade check if variable exists example in laravel. There are many ways in checking if the variable exists or not in laravel blade file. So let's see the example code:
Check whether the variable is set or not in laravel blade:
@isset($user)
{{ $user }}
@endisset
Check whether the variable is set or not in laravel blade:
@if(isset($user))
{{ $user }}
@endif
Check whether the variable is empty or not in laravel blade:
@empty($users)
{{ $users }}
@endempty
Check whether the variable is set or not in laravel blade:
{{ $user ?? '' }}
Hope it can help you.
#laravel #laravel-9x #blade-directive