Check if a variable exists in Laravel Blade templates
July 20, 2022 ‐ 1 min read
This might now be a common use-case, but let's say for example you need to check if a Laravel Blade layout template whether a variable exists.
In regular PHP code you can use the isset()
function for this. In Blade there is a directive which allows for such a check.
@isset($obj)
{{ $obj }}
@endisset
In case you need to check whether a variable exists in conjunction with another check you can just use the regular @if
directive.
@if(isset($obj) && gettype($ob) === 'object')
{{ $obj }}
@endif