Conditional statements in Laravel for Beginners


🧩 Blade Conditional Directives in Blade files (HTML + PHP)
Imagine you're building a webpage where you want to display different content based on certain conditions—like showing a special message to logged-in users or displaying a list of products only if they exist. Blade's conditional directives make this easy and clean.
🔄 1. @if
, @elseif
, @else
, and @endif
These directives allow you to execute code based on conditions.
Example:
<!-- resources/views/welcome.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome Page</title>
</head>
<body>
@php
$user = 'admin'; // Change this to test different outputs
@endphp
@if ($user == 'admin')
<h1>Welcome, Admin!</h1>
@elseif ($user == 'editor')
<h1>Welcome, Editor!</h1>
@else
<h1>Welcome, Guest!</h1>
@endif
</body>
</html>
Output:
If
$user
is'admin'
:Welcome, Admin!
If
$user
is'editor'
:Welcome, Editor!
Otherwise:
Welcome, Guest!
🚫 2. @unless
This is the inverse of @if
. It executes the code inside unless the condition is true.
Example:
<!-- resources/views/welcome.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome Page</title>
</head>
<body>
@php
$isAdmin = false; // Change this to test different outputs
@endphp
@unless ($isAdmin)
<p>You are not an admin.</p>
@endunless
</body>
</html>
Output:
If
$isAdmin
isfalse
:You are not an admin.
If
$isAdmin
istrue
: No output.(Laravel)
🧠 3. @isset
and @empty
@isset
: Checks if a variable is set and not null.@empty
: Checks if a variable is empty.
Example:
<!-- resources/views/welcome.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome Page</title>
</head>
<body>
@php
$email = 'user@example.com'; // Change this to test different outputs
@endphp
@isset($email)
<p>Email is set: {{ $email }}</p>
@endisset
@empty($email)
<p>Email is empty.</p>
@endempty
</body>
</html>
Output:
If
$email
is'user@example.com'
:Email is set: user@example.com
If
$email
isnull
:Email is empty.
🔄 4. @switch
, @case
, @default
, and @endswitch
These are used for multiple conditions, similar to a switch-case statement.
Example:
<!-- resources/views/welcome.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome Page</title>
</head>
<body>
@php
$role = 'admin'; // Change this to test different outputs
@endphp
@switch($role)
@case('admin')
<p>Welcome, Admin!</p>
@break
@case('editor')
<p>Welcome, Editor!</p>
@break
@default
<p>Welcome, Guest!</p>
@endswitch
</body>
</html>
Output:
If
$role
is'admin'
:Welcome, Admin!
If
$role
is'editor'
:Welcome, Editor!
Otherwise:
Welcome, Guest!
✅ Recap
@if
,@elseif
,@else
,@endif
: Standard conditional checks.@unless
: Executes code unless the condition is true.@isset
: Checks if a variable is set and not null.@empty
: Checks if a variable is empty.@switch
,@case
,@default
,@endswitch
: Used for multiple conditions.
Subscribe to my newsletter
Read articles from Nature directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
