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.

#laravel #laravel-8x