Laravel Blade is a templating engine in Laravel which is very simple and yet way more pwerful to use. It which allows you to even use plain PHP code into your code file which most of the blade templating engine fails to do so. In Laravel Blade, views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. And that’s the beauty of using the Laravel templating engine.
In this article i am going to tell you about the 4 awesome Laravel Blade Directive which you can use into your application and can simplify the process of developing of cool laravel application. So let’s begin
4 Awesome Laravel Blade Directive To Use
Blade uses .blade.php extension to begin with and is stored in the resources/views of the your installed Laravel application .
#1 – Check If User is authenticated
In the laravel we use the following code to check if the user is authenticated or not
@if(auth()->user()) But in the Laravel Blade directive we can use something like this to avoid the above code.
// The user is authenticated.
//the user is authenticated
#2- Use Plain PHP in Your Blade View
To use the plain PHP in the Laravel blade we can use either something like this to begin with
@php Isn’t it awesome to have been able to work with the plain PHP code in the Laravel Blade directive.
//your php code goes here
#3 – View Based on the condition
Conditionally including a view based on a condition is useful when you only want to add content based on logic like an authenticated user.
You could write this using an
# 4- Render JSON In Laravel Blade
Earlier to encode the JSON reponse from your API call or from your code you to manually call the JSON code However, instead of manually calling
json_encode, you may use the
@json Blade directive:
<span class="token markup"><span class="token tag"><span class="token punctuation"><</span>script<span class="token punctuation">></span></span></span>
<span class="token keyword">var</span> app <span class="token operator">=</span> @<span class="token function">json<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token markup"><span class="token tag"><span class="token punctuation"></</span>script<span class="token punctuation">></span></span></span>
You can learn about these and other useful tricks in the official Blade documentation for improving your frontend templating in a Laravel project.
Happy Coding !