Avoid json_encode() and Use @json() Blade Directive in Laravel

In this example, I will discuss json encode in laravel blade in detail. Sometimes you may pass an array to your view with the intention of rendering it as JSON in order to initialize a JavaScript variable. That time what we do ? Normally we use json_encode() method as like below.

 

But do you know Laravel has an awesome blade directive for json_encode. You can use @json for the same task that you do with json_encode. Instead of manually calling json_encode, you may use the @json Blade directive. The @json directive accepts the same arguments as of PHP's json_encode function:

 

Hope it can help you.

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js