Laravel Blade Isset Else Example

Laravel Blade Isset Else Example

Laravel is a popular PHP framework known for its elegant syntax and powerful tools for web development. One of these tools is Blade, Laravel’s simple yet powerful templating engine. Blade allows you to write clean and readable HTML templates with minimal PHP code. In this article, we will explore the @isset and @else directives in Blade, which help you handle variables and their default values.

Understanding @isset

The @isset directive in Blade is used to check if a variable is set and is not null. This is useful when you want to display content only if a certain variable is defined. Here’s the basic syntax:

@isset($variable)
// The variable is set and is not null
@endisset

If the variable $variable is set, the code inside the @isset block will be executed. If the variable is not set, the code inside the block will be skipped.

Using @isset with @else

Often, you’ll want to provide an alternative action or display default content when a variable is not set. This is where the @else directive comes in. Here’s how you can use @isset with @else:

@isset($variable)
<!-- Content to display if the variable is set -->
<p>{{ $variable }}</p>
@else
<!-- Content to display if the variable is not set -->
<p>Variable is not set.</p>
@endisset

This code will check if $variable is set. If it is, it will display the value of $variable. If not, it will display “Variable is not set.”

Practice Code Example

Let’s look at a more detailed example. Suppose you have a Blade template for a user profile page. You want to display the user’s name if it is set, or a default message if the name is not available.

Controller

First, let’s set up a controller to pass the user’s name to the view. Create a controller called UserController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
public function showProfile()
{
// Example user data
$user = [
'name' => 'John Doe', // You can change this to null or remove it to test the @else block
];

return view('profile', compact('user'));
}
}

Blade Template

Next, create a Blade template called profile.blade.php in the resources/views directory

<!DOCTYPE html>
<html>
<head>
<title>User Profile</title>
</head>
<body>
<h1>User Profile</h1>

@isset($user['name'])
<p>Name: {{ $user['name'] }}</p>
@else
<p>Name is not available.</p>
@endisset

</body>
</html>

Routes

Finally, define a route to display the profile page. Open the routes/web.php file and add the following route:

use App\Http\Controllers\UserController;

Route::get('/profile', [UserController::class, 'showProfile']);

Testing the Example

To test this example, run your Laravel application and visit http://your-app-url/profile in your web browser. You should see the user’s name displayed if it is set. If you change the name in the controller to null or remove it, you will see the default message “Name is not available.”

Summary

In this article, we learned how to use the @isset and @else directives in Laravel Blade templates. These directives allow you to check if a variable is set and provide alternative content if it is not. By using these directives, you can create more dynamic and robust views in your Laravel applications.

Blade makes it easy to write clean and readable templates, and understanding these directives will help you handle variables more effectively in your views. Happy coding!