How to write custom if statements in Laravel?

In this article I will create custom blade directive for blade template in Laravel.

Laravel blade overview

Blade is Laravel’s easy-to-use, high-performance templating tool.

We can write simple PHP code inside our blade templates.

In the case of blade template files, the file extension is .blade.php and they are usually located in resources/views directory.

We can use the global view helper to return blade views from routes or controllers.

Blade also offers easy-to-use shortcuts for PHP control structures like conditional statements and loop.

These shortcuts provide a very clean and concise way to work with PHP control structures.

We can get more details on blade templates at https://laravel.com/docs/10.x/blade.

Now we will create a custom if directive for the template in Laravel.

The if and unless directive

We can use the if, else, and endif directives to construct if statements.

Blade also provides @unless conditional directive.

Below are examples of @if and @unless directives that we can use in the blade.php file.

Example of In-built if and unless directive

@if (count($records) === 1)
    I have one record!
@elseif (count($records) > 1)
    I have multiple records!
@else
    I don't have any records!
@endif
@unless (Auth::check())
    You are not signed in.
@endunless

Now we will learn how to create custom if statements.

Custom if and unless directive

Blade has a method called Blade::if, which will allow us to define custom conditional directives using closures.

We will define a custom condition that will check the default filesystem disk that is used by the framework and is set in config\filesystems.php.

Steps to create custom if and unless directive

We can create custom directives in Laravel by following the steps mentioned below.

Step 1: Modify the AppServiceProvider.php

For that, first of all, we will modify the boot method in app\Providers\AppServiceProvider.php. The modified code of the AppServiceProvider.php would be as follows.

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
class AppServiceProvider extends ServiceProvider
{  
    public function register(): void
    {
        //
    }    
    public function boot(): void
    {
        Blade::if('disk', function ($value) {
		return config('filesystems.default') === $value;
	});
    }
}

Here we have defined a custom disk directive that will check the default filesystem disk that is used by the application. The default filesystem disk is set in default property of config\filesystems.php file that we can see in the following piece of code.

<?php

return [

'default' => env('FILESYSTEM_DISK', 'local'),

// ...

];

In our application default default filesystem disk is local.

Step 2: Create route

Now, we will create a route in routes\web.php.

<?php
use Illuminate\Support\Facades\Route;

Route::get('/customdirective', function () {
    return view('customdirective');
});
Step 3: Create view file

Now, we will create customdirective.blade.php in resources\views directive. The code of this php file will be as follows.

@disk('local')
Local
@elsedisk('s3')
S3
@else
Other
@enddisk
@unlessdisk('local')
Other
@enddisk
Step 4: Browser output

Finally, if we browse http://<SITEURL>/customdirective the output will be: Local as default filesystem disk is local in our application.

1 thought on “How to write custom if statements in Laravel?”

  1. Pingback: Webtechbased

Comments are closed.