Check If Variable Exists with Laravel Blade Directive

Laravel Blade directives are used to manage the display of your content. There are a variety of directives available, which allow you to control everything from the data that is displayed to the style and layout of your content.

Before printing a variable, it’s important to first check whether or not the variable exists. This can be done via different methods. This can be helpful when you want to avoid displaying errors on your page or when you want to prevent certain variables from being displayed.

@isset($phoneName)
    {{ $phoneName}}
@endisset

@if(empty($user))
    <p>Guest</p>
@else
    <p>Hello, {{ $user->name }}</p>
@endif

//Laravel 5.7+: print quantity if it exists, otherwise print 0.00
{{ $qty ?? '0.00' }}
{{ $qty or '0.00' }}

@if(isset($student))
    {{ $student->name }}
@endif

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.

Close