5 useful Laravel directives. Sometimes in laravel views, you have to write certain logic: checking for an admin, or a regular user, checking their rights. However, thanks to the built-in directive creation capabilities, this process can be made more convenient by taking all the validation logic into directives.
If you are new to learning Laravel, luckily enough, there are already quite a few built-in blade templating directives out there. The creators of the framework took care of solving the most demanded tasks and programmed them into the core of the framework in the form of directives. This article will walk you through 5 useful directives for a Laravel programmer.
Table of Contents
How to check authentication in blade Laravel
When verifying user authentication, a newcomer Laravel developer would do something similar to this:
@if(Auth::user())
@endif
Or using a helper, which is, in fact, a wrapper over the code above:
@if(auth()->user())
@endif
You can read more about 5 useful helpers in the previous article.
However, the framework has a built-in directive auth for this purpose:
@auth
@endauth
How to check the guest status in Laravel
In addition to verifying user authentication, it is also common to check if the current visitor is a guest.
This is how a native check would look like:
@if(auth()->guest())
@endif
In contrast to this code, there is a directive guest:
@guest
@endguest
How to attach blade template if it exists – otherwise different
When creating a site with many skins and styles, there are multiple checks for the existence of blade patterns. And it often happens that you need to check if a template file exists in this theme, otherwise, connect the default template.
To provide for this, we will write a condition:
@if(view()->exists(‘custom-view’))
@include(‘custom-view’)
@else
@include(‘default-view’)
@endif
But, it would be much more concise to use the directive include first:
@includeFirst([‘custom-view’, ‘default-view’]);
How to connect a blade template based on a condition
Connecting a view, depending on the condition, is sometimes very useful. It helps a lot when you need to display content, depending on a certain dynamic condition.
For example, if a user has achievements or posts, then connect the appropriate component:
@if($user->hasAchives())
@include(‘user.achieves’)
@endif
But, it’s easier using the directive includeWhen:
@includeWhen($user->hasAchives(), ‘user.achieves’);
Which, if the condition is met $user->hasAchives() == true, will connect the view user.achieves
How to check and connect an existing view
Again, returning to the topic of customizing templates and creating a multi-theme system, one cannot but touch upon the function of checking the existence of views.
To check if a topic exists, write:
@if(view()->exists(‘custom-view’))
@include(‘custom-view’)
@endif
But, it is much easier to describe this condition with the directive includeIf
@includeIf(‘custom-view’)
All checks and further connections were simply reduced to one line of code. Isn’t it a miracle?
There is no third?
In addition to the above-considered options for using directives (checking compliance with the condition – auth, guest), the functionality of directives does not end there. It turns out that it is possible to check the condition, and if it is not met, follow the second scenario (if (…) {} else {}).
Guest check:
@guest
@else
@endguest
Or with authentication:
@auth
@else
@endauth
Summary
As a result, useful directives were considered to simplify working with the framework in blade views. It is convenient and practical, this is one of the ways of following correct programming: transferring the logic of checks from views to the framework itself. Now, if the logic for checking authentication or rights suddenly changes, then you can easily redefine the directives by changing them in one place, instead of roaming around all the views, changing all the checks that were hard-coded. So using directives and implementing them correctly brings many more benefits than just shrinking code.
In order to study in more detail all the built directives, I advise you to read the documentation.
For professional services or consultation from GCC Marketing Dubai please Contact Us or call us directly on 00971567300683