Check if a Variable Exists with Laravel Blade Directive

Checking whether a variable exists or not is an important step before printing its value in a view.

If you print an un-declared variable, Laravel will generate errors.

There are 4 approaches in Blade.

@isset Directive

    {{ $car_name}}

@empty Directive

    <p>Welcome, {{ $user }}</p>

Ternary Operator

Starting from Laravel 5.7, we can use the ternary operator to check a variable’s existence. Both ?? and or operators are allowed.

//print price if it exists, otherwise print 0.00
{{ $price ?? '0.00' }}
{{ $price or '0.00' }}

if isset() Method

    {{ $client->name }}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.