How to use Blade directives like @if, @else, @elseif, and @endif in Laravel?

To use Blade directives like @if, @else, @elseif, and @endif in Laravel, you can follow these steps:

  1. Open the blade template file where you want to use the directives (e.g., home.blade.php).

  2. Use the syntax @if(condition) to start an if statement. Replace "condition" with the desired condition you want to evaluate.

Example:

@if($user->isAdmin) {{-- Display content for an admin user --}} @endif
  1. Add the desired content or HTML within the if statement.

Example:

@if($user->isAdmin) <h1>Welcome, Admin!</h1> @endif
  1. Use the @else directive to define alternative content that will be displayed if the condition in the @if statement evaluates to false.

Example:

@if($user->isAdmin) <h1>Welcome, Admin!</h1> @else <h1>Welcome, Guest!</h1> @endif
  1. Use the @elseif directive to evaluate additional conditions.

Example:

@if($user->isAdmin) <h1>Welcome, Admin!</h1> @elseif($user->isEditor) <h1>Welcome, Editor!</h1> @else <h1>Welcome, Guest!</h1> @endif
  1. Close the if statement using the @endif directive.

Example:

@if($user->isAdmin) <h1>Welcome, Admin!</h1> @elseif($user->isEditor) <h1>Welcome, Editor!</h1> @else <h1>Welcome, Guest!</h1> @endif

That's it! You can use these Blade directives to conditionally display content based on the conditions you specify. Remember to replace $user->isAdmin, $user->isEditor, and any other conditions with your actual variables or conditions.