Share this:

Today we are going to learn Laravel 10: How to Set Variables in a Laravel Blade Template. Laravel is a popular PHP framework known for its elegant syntax and powerful features.

One of its key features is the Blade templating engine, which allows developers to build dynamic and reusable templates.

In this blog post, we will explore how to set variables in a Laravel Blade template, specifically focusing on the latest version, Laravel 10.

Also Read : How to use multiple databases in Laravel

Understanding Blade Templates:

Before we dive into setting variables in a Laravel Blade template, let’s briefly discuss what Blade templates are. Blade is a simple yet powerful templating engine provided by Laravel. It allows developers to write clean and readable code by separating HTML markup from PHP logic. Blade templates have a “.blade.php” file extension and are stored in the resources/views directory by default.

1. Passing Variables from Controller to View:

In Laravel, you can pass variables from your controller to your Blade template using the with method or by compacting an array of variables. Let’s see how it’s done.

Using the with Method:

public function index()
{
    $message = 'Welcome to my blog!';
    return view('index')->with('message', $message);
}

In this example, we pass a variable called $message to the index view using the with method. Inside the Blade template, you can access this variable using the following syntax:

<h1>{{ $message }}</h1>

Using the Compact Function:

public function index()
{
    $message = 'Welcome to my blog!';
    $title = 'My Blog';
    return view('index', compact('message', 'title'));
}

In this case, we use the compact function to create an array of variables ($message and $title) and pass them to the index view. The variables can then be accessed in the Blade template as follows:

<h1>{{ $message }}</h1>
<p>{{ $title }}</p>

2. Setting Variables within Blade Templates:

In addition to passing variables from controllers, Laravel also allows you to set variables directly within Blade templates. This feature can be useful when you need to perform calculations or modify data before displaying it.

To set a variable within a Blade template, you can use the @php directive. Here’s an example:

@php
    $name = 'John Doe';
    $age = 30;
    $isStudent = true;
@endphp

In this example, we set three variables: $name, $age, and $isStudent. These variables can be accessed and used within the template as needed.

3. Conditionally Setting Variables:

In some cases, you may need to conditionally set variables based on certain criteria. Laravel Blade provides various conditional directives to accomplish this.

For instance, you can use the @if directive to set a variable based on a condition:

@if($isAdmin)
    @php
        $accessLevel = 'Admin';
    @endphp
@else
    @php
        $accessLevel = 'User';
    @endphp
@endif

In this example, the $accessLevel variable is conditionally set based on the value of the $isAdmin variable.

Also Read : Laravel – Eloquent “Has”, “With”, “WhereHas” – What Do They Mean?

Conclusion:

Setting variables in Laravel Blade templates is a fundamental skill for building dynamic and interactive web applications.

In this blog post, we explored two methods of passing variables from controllers to views, as well as how to set variables directly within Blade templates using the @php directive.

We also touched on conditional variable setting using Blade’s conditional directives.

Laravel 10 continues to enhance the templating experience with new features and improvements. By mastering variable handling in Blade templates, you can create expressive and flexible views in your Laravel applications.

Remember, proper use of variables in Blade templates helps maintain clean, organized, and maintainable code. Happy coding with Laravel 10!

Share this:

Categorized in: