Sunday, November 3

4 Awesome Laravel Blade Directive To Use

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())
// The user is authenticated.
@endif
   But in the Laravel Blade directive we can use something like this to avoid the above code.


@auth
//the user is authenticated
@endauth

#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
//your php code goes here
@endphp
   Isn’t it awesome to have been able to work with the plain PHP code in the Laravel Blade directive.

 READ MORE :- LARAVEL 5.6.30 [SECURITY RELEASE] HERE’S WHAT’S NEW !

#3 – VIEW BASED ON THE CONDITION

View Based on the condition
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 @if condition:

@if($post->hasComments())
@include('posts.comments')
@endif

# 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:

<script>
    var app = @json($array);
</script>

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 !

Article Source : https://techanical-atom.com

The post 4 Awesome Laravel Blade Directive To Use appeared first on Laravel Interview Questions.


Source: Laravelinterviewquestions.com

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x