Laravel / Laravel Views and Blade Templates

Using Blade Directives for Conditionals

This tutorial covers how to use Blade directives to control conditional logic within your views. We will learn how to use Blade's if, else, and unless directives to display differ…

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Covers building dynamic views using Blade templating engine.

Using Blade Directives for Conditionals

1. Introduction

In this tutorial, we will explore how to use Blade directives to manage conditional logic within your views in Laravel. Blade is a powerful templating engine provided with Laravel.

You will learn how to use @if, @else, @elseif, and @unless directives to display content based on certain conditions.

Prerequisites: Familiarity with basic PHP and Laravel will be beneficial.

2. Step-by-Step Guide

Blade offers several directives that you can use to handle conditional statements within your views. These are @if, @else, @elseif, and @unless.

The @if directive functions in a similar manner to PHP's if statement. It executes some code if a certain condition is true.

The @else directive functions as PHP's else. It executes some code if the preceding if condition is not met.

The @elseif directive, as the name suggests, combines else and if. It checks another condition if the preceding if condition is not met.

The @unless directive is the opposite of if. It executes some code if a certain condition is not true.

3. Code Examples

Here are some examples demonstrating how to use these directives.

Example 1: Using @if directive

@php 
    $user = Auth::user();
@endphp

@if($user->isAdmin())
    <p>You are an admin.</p>
@endif

In this example, we first get the authenticated user. We then check if the user is an admin. If true, it displays "You are an admin."

Example 2: Using @if, @else directives

@php 
    $user = Auth::user();
@endphp

@if($user->isAdmin())
    <p>You are an admin.</p>
@else
    <p>You are not an admin.</p>
@endif

In this example, we added an @else directive. If the user is not an admin, it displays "You are not an admin."

Example 3: Using @if, @elseif, @else directives

@php 
    $user = Auth::user();
@endphp

@if($user->isAdmin())
    <p>You are an admin.</p>
@elseif($user->isEditor())
    <p>You are an editor.</p>
@else
    <p>You are a regular user.</p>
@endif

In this example, we added an @elseif directive. If the user is not an admin but is an editor, it displays "You are an editor."

Example 4: Using @unless directive

@php 
    $user = Auth::user();
@endphp

@unless($user->isAdmin())
    <p>You are not an admin.</p>
@endunless

In this example, we use the @unless directive. It displays "You are not an admin." unless the user is an admin.

4. Summary

In this tutorial, you learned how to use Blade directives to handle conditional logic in your views. You saw how to use @if, @else, @elseif, and @unless to control what content is displayed based on certain conditions.

Next Steps: You can now experiment with these directives in your views. Try to create more complex conditions using these directives.

Additional Resources:
- Laravel Blade Documentation
- Laravel Blade Templating – A Complete Guide

5. Practice Exercises

Exercise 1: Create a view that displays different messages based on the user's role (admin, editor, user).

Exercise 2: Create a view that uses the @unless directive to display a message only to non-admin users.

Exercise 3: Create a view that uses multiple @elseif directives to display different messages for different user roles.

Solutions:
Please refer to the above code examples and modify them according to the requirements of these exercises. Remember, practice is key in mastering these concepts. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Meta Tag Analyzer

Analyze and generate meta tags for SEO.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help