Laravel Laravel 5.5

How to Use Laravel 5.5 BladeIf Directive

How to Use Laravel 5.5 BladeIf Directive
Written by Yogesh Koli

Laravel 5.5 next big released is scheduled on to July 2017, that is going to have more advanced features.

Laravel 5.5 is going to have new Blade directive called, BladeIf, it is kind of custom condition where you can check many things like if you want to check something before displaying to user ex. check user is authenticated or not, if user is authenticated then display something something like that.

You might thinking that we can do that on blade template itself but as we know we needs to use this kind things many time and instead of repeating the statements now Laravel 5.5 going to provide more custom way to defined conditions using Blade::if directive

Let’s see how can we do that using Laravel 5.5

Install Laravel 5.5 dev version:

As Laravel 5.5 is not realsed yet, let’s grab that in using –dev command, you can use following command to install new Laravel 5.5 project with dev version, please do not use it for production for now until we get the official released version

As this is the development version of Laravel we have to generate  application encryption key:

Now we are ready to go to test Laravel 5.5 Blade::if directive

How to use Laravel 5.5 BladeIf directive?

To use Laravel Blade::if directive into your laravel 5.5 project, you have to add following line of code into your App ServiceProvider class under boot() method:

app/Providers/AppServiceProvider.php

So if you see the above example we are simply checking that the given user is subscribed or not and now you can easily check that out into your blade template:

Here is the test on blade template using above blade if directive:

resources/views/welcome.blade.php

So if you run the above project now you should see the message User is not subscribed as we don’t have any user or any subscription because this is the just a demo application to get you understand how you can implement Laravel 5.5 Blade::if feature into your project.

BladeIf Directive Using Parameter:

Yes, blade if directive can have parameters as well, so let’s take example of using laravel blade if directive along with the parameters:

In the following example we are going to have a blade if directive to checkout the environment and depending on the running environment (local or production) we would be adding the scripts into our blade,  we often needs to do that for example we may have a script which needs to be only run on when the project is on production server, so let’s take a simple example of it:

Create new Laravel Blade if Directive:

app/Providers/AppServiceProvider.php

Use newly created blade if directive on blade template:

resources/views/welcome.blade.php

You can also checkout more stuff on – Laravel

If you any question related to this tutorial, let me know using comment box below.

About the author

Yogesh Koli

Yogesh Koli is a software engineer & a Blogger lives in India. He’s driven by an addiction to learning and a love for adventure. he has 5+ years of experience working with the front-end, back-end, web application development, and system design.