Laravel Check if Variable Exist in Blade (View) Directive

Last modified on May 9, 2020 42 sec read

HIRE US 🖐
We're available to do freelance project. Take a look at our services!

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

  1. Laravel @isset Directive
  2. Laravel @empty Directiv
  3. Laravel if isset() Method
  4. Laravel Ternary Operator

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. 🙂

Author

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.

Follow

Leave a Reply

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