10 Blade directives that you should know

Daan
3 min readMay 16, 2019

Out of the box Laravel comes with Blade, a powerful templating engine that makes writing your views in Laravel very easy. Most Laravel developers know a lot about the framework, but their Blade knowledge is lacking sometimes. In this article I want to share ten Blade directives with you that are very useful and could be used to make your life as a developer easier.

@json

Instead of manually callingjson_encode($array) in your views you may use the json directive:

@json($array)

If you are using Vue.js for your front end the json directive comes in very handy when seeding Vue-components from your Blade template.

<blog-overview-component :blogs='@json($blogs)'>
</blog-overview-component>

@isset

A common suboptimal way to check if a variable is set in Blade is by using the if directive.

@if(isset($blog))
// $blog is defined and is not null
@endif

This could be done via a shortcut, which looks much cleaner and is more readable.

@isset($blog)
// $blog is defined and is not null
@endisset

The same thing could be done with the empty directive.

@if (empty($blogs))
// $blogs is empty
@endif

--

--