Laravel Conditional Directives


What are conditional directives in Blade?

Conditional directives in Blade allow you to handle control flow based on certain conditions. These directives function similarly to PHP's conditional statements, but with a cleaner, more expressive syntax for use within Blade templates. Blade offers built-in conditional directives such as @if, @elseif, @else, and @unless.


How do you use the @if directive in Blade?

The @if directive in Blade is used to check a condition and display content if the condition is true. It works similarly to PHP's if statement but is more readable in Blade templates.

Example of using @if:

@if($user->isAdmin())
    <p>Welcome, Admin!</p>
@endif

In this example, if the $user->isAdmin() condition is true, the message "Welcome, Admin!" will be displayed.


How do you use the @elseif directive in Blade?

The @elseif directive is used to check an additional condition if the previous @if condition is false. It is similar to PHP's elseif statement.

Example of using @elseif:

@if($user->isAdmin())
    <p>Welcome, Admin!</p>
@elseif($user->isModerator())
    <p>Welcome, Moderator!</p>
@endif

In this example, if the user is not an admin but is a moderator, the message "Welcome, Moderator!" will be displayed.


How do you use the @else directive in Blade?

The @else directive is used to provide a fallback if all the previous conditions are false. It works similarly to PHP's else statement.

Example of using @else:

@if($user->isAdmin())
    <p>Welcome, Admin!</p>
@else
    <p>Welcome, User!</p>
@endif

In this example, if the user is not an admin, the message "Welcome, User!" will be displayed.


How do you use the @unless directive in Blade?

The @unless directive is the inverse of @if. It checks if a condition is false and executes the block of code if the condition is not met.

Example of using @unless:

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

In this example, if the user is not an admin, the message "You are not an admin." will be displayed.


How do you use the @isset directive in Blade?

The @isset directive checks if a given variable is defined and not null. It is the Blade equivalent of PHP's isset() function.

Example of using @isset:

@isset($user)
    <p>User name: {{ $user->name }}</p>
@endisset

In this example, the user's name will be displayed only if the $user variable is set.


How do you use the @empty directive in Blade?

The @empty directive checks if a given variable is empty. It is the Blade equivalent of PHP's empty() function.

Example of using @empty:

@empty($posts)
    <p>No posts available.</p>
@endempty

In this example, if the $posts variable is empty, the message "No posts available." will be displayed.


How do you use the @switch directive in Blade?

The @switch directive provides a switch-case-like structure in Blade. It allows you to evaluate a variable against different cases and execute a block of code based on the value of the variable.

Example of using @switch:

@switch($user->role)
    @case('admin')
        <p>Welcome, Admin!</p>
        @break

    @case('moderator')
        <p>Welcome, Moderator!</p>
        @break

    @default
        <p>Welcome, User!</p>
@endswitch

In this example, the switch statement checks the $user->role value and displays a corresponding message based on the role. If none of the cases match, the default message "Welcome, User!" is displayed.


How do you use the @auth and @guest directives in Blade?

The @auth directive checks if the user is authenticated, while the @guest directive checks if the user is a guest (not authenticated). These directives are useful for displaying different content based on the user's authentication status.

Example of using @auth:

@auth
    <p>Welcome, authenticated user!</p>
@endauth

Example of using @guest:

@guest
    <p>Please log in to access this content.</p>
@endguest

In these examples, content is conditionally displayed based on whether the user is authenticated or not.


How do you use custom conditional directives in Blade?

In Laravel, you can define custom conditional directives using the Blade::if() method. These custom conditionals can then be used just like the built-in ones.

Example of creating a custom conditional directive:

Blade::if('isAdmin', function ($user) {
    return $user->isAdmin();
});

Usage in a Blade template:

@isAdmin($user)
    <p>Welcome, Admin!</p>
@else
    <p>You are not an admin.</p>
@endisAdmin

In this example, the custom @isAdmin directive checks if the user is an admin and displays content accordingly.


How do you use the @production and @env directives in Blade?

The @production directive is used to check if the application is running in the production environment, while the @env directive can be used to check if the application is running in any specified environment.

Example of using @production:

@production
    <p>This is production mode.</p>
@endproduction

Example of using @env:

@env('local')
    <p>This is the local environment.</p>
@endenv

In these examples, content is displayed based on the application environment.

Ads