Laravel Check if Variable Exist in Blade (View) Directive

Published on Mar 29, 2020

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

Comments (0)