Posted: 23-09-2017

Laravel 5.5 new blade directives recap

Laravel 5.5 new blade directives recap

The recent Laravel 5.5 release comes with some usefull blade helper and directives.

@includeFirst Blade directive (from laravel 5.5.5)

Now  the  following code:


Can be replaced by a single line of code:

@includeFirst(['custom-template', 'default-template'])



Now  Laravel 5.5, you'll find a new syntax for defining custom if Blade directives.

The syntax might something like this in your AppServiceProvider::boot() method:

\Blade::if('is_subscribed', function () {
   return auth()->check() && auth()->user()->isSubscribed();

and in Blade

   User is Subscribed.
   User is not Subscribed.

Blade::If() Directive can also has Parameter

Blade::if ('environment', function ($environment)
  return app()->environment($environment);

and in blade template

  your  code goes here......

An indepth tutorial about this new featurs can be found at Laracast video Custom Blade "If" Directives.

@auth and @guest Directives

These new shortcuts permit  to quickly determine if the current user is authenticated or is a guest: 

guest stuff here
logged user stuff here


   logged user stuff here

or if you're using a Guard

    logged in through admin guard

Rendering JSON @json

Sometimes you may pass an array to your view with the intention of rendering it as JSON in order to initialize a JavaScript variable. For example:

    var app = json-decode($array) ;

can be replaced by:

    var app = @json($array);

Learning More About Laravel new Blade directives @ What's New in Laravel 5.5: New Blade Directives