Laravel 5.5 new feature – Custom Blade::if Directives with example

Laravel 5.5 new feature – Custom Blade::if Directives with example

In this post we will give you information about Laravel 5.5 new feature – Custom Blade::if Directives with example. Hear we will give you detail about Laravel 5.5 new feature – Custom Blade::if Directives with exampleAnd how to use it also give you demo for it if it is necessary.

In this tutorial, I will tell you the new features custom Blade::if directives added with Laravel 5.5 with example.

Sometime you write same if condition everywhere in the view but now you do not write same if condition many time by using new feature Blade::if directive of Laravel 5.5.

Laravel 5.5 Blade::if make it more convenient to abstract repetitive checks out of templates and making them cleaner and more readable.

There is a boot method inside the AppServiceProvider class to write your logic for blade if directive.


Here in this class, We will write a logic to check the apps environment inside the boot method :


  1. <?php
  2. namespace AppProviders;
  3. use IlluminateSupportServiceProvider;
  4. use IlluminateSupportFacadesBlade;
  5. class AppServiceProvider extends ServiceProvider
  6. {
  7. /**
  8. * Bootstrap any application services.
  9. *
  10. * @return void
  11. */
  12. public functionboot()
  13. {
  14. Blade::if('env',function($env){
  15. returnapp()->environment($env);
  16. });
  17. }
  18. /**
  19. * Register any application services.
  20. *
  21. * @return void
  22. */
  23. public functionregister()
  24. {
  25. }
  26. }

namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateSupportFacadesBlade;

class AppServiceProvider extends ServiceProvider
     * Bootstrap any application services.
     * @return void
    public function boot()
        Blade::if('env', function ($env) {
            return app()->environment($env);

     * Register any application services.
     * @return void
    public function register()


Welcome view

Now open your welcome.blade.php file to use your new blade directive in following way :


  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <title>Laravel 5.5 new feature</title>
  5. <metacharset="utf-8">
  6. <metahttp-equiv="X-UA-Compatible"content="IE=edge">
  7. <metaname="viewport"content="width=device-width, initial-scale=1">
  8. </head>
  9. <body>
  10. @env('local')
  11. You are in the local environment..
  12. @endenv
  13. </body>
  14. </html>
<!DOCTYPE html>
    <title>Laravel 5.5 new feature</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
        You are in the local environment..

There are also some other example to understand the concept of Blade::if directive.

Let’s have a another example to check if user is subscribed or not.

  1. Blade::if('is_subscribed',function(){
  2. returnauth()->check()&&auth()->user()->isSubscribed();
  3. });
Blade::if('is_subscribed', function () {
    return auth()->check() && auth()->user()->isSubscribed();

Use this new directive in template :

  1. @is_subscribed
  2. User is Subscribed.
  3. @else
  4. User is not Subscribed.
  5. @endis_subscribed
    User is Subscribed.
    User is not Subscribed.

There are many condition where you can use Blade::if directive, You can check user is logged in the application or not.

Label :


Laravel PHP Framework

How To


Web Development

Hope this code and post will helped you for implement Laravel 5.5 new feature – Custom Blade::if Directives with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

8  +    =  10

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US