Laravel Check if Variable Exist in Blade (View) Directive
In this short article, I’m going to share some ways to check if variable exist in the blade file. Let’s see:
Table of Contents
Laravel @isset Directive
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
@isset($variable)
{{ $variable }}
@endisset
</body>
</html>
Laravel @empty Directive
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
@empty($variable)
{{ $variable }}
@endempty
</body>
</html>
Laravel if isset() Method
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
@if(isset($variable))
{{ $variable }}
@endif
</body>
</html>
Laravel Ternary Operator
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<!-- way 1 -->
{{ $varible ?? 'default' }}
<!-- way 2 -->
{{ $variable or "default" }}
</body>
</html>
That’s all. Thank you.
Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.