Laravel 5.5 new feature – Custom Blade::if Directives with example
In this post we will give you information about Laravel 5.5 new feature – Custom Blade::if Directives with example. Hear we will give you detail about Laravel 5.5 new feature – Custom Blade::if Directives with exampleAnd how to use it also give you demo for it if it is necessary.
In this tutorial, I will tell you the new features custom Blade::if
directives added with Laravel 5.5 with example.
Sometime you write same if condition everywhere in the view but now you do not write same if condition many time by using new feature Blade::if
directive of Laravel 5.5.
Laravel 5.5 Blade::if make it more convenient to abstract repetitive checks out of templates and making them cleaner and more readable.
There is a boot method inside the AppServiceProvider
class to write your logic for blade if directive.
AppServiceProvider
Here in this class, We will write a logic to check the apps environment inside the boot method :
app/Providers/AppServiceProvider.php
- <?php
- namespace AppProviders;
- use IlluminateSupportServiceProvider;
- use IlluminateSupportFacadesBlade;
- class AppServiceProvider extends ServiceProvider
- {
- /**
- * Bootstrap any application services.
- *
- * @return void
- */
- public functionboot()
- {
- Blade::if('env',function($env){
- returnapp()->environment($env);
- });
- }
- /**
- * Register any application services.
- *
- * @return void
- */
- public functionregister()
- {
- }
- }
<?php namespace AppProviders; use IlluminateSupportServiceProvider; use IlluminateSupportFacadesBlade; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Blade::if('env', function ($env) { return app()->environment($env); }); } /** * Register any application services. * * @return void */ public function register() { } }
Welcome view
Now open your welcome.blade.php
file to use your new blade directive in following way :
resources/views/welcome.blade.php
- <!DOCTYPEhtml>
- <html>
- <head>
- <title>Laravel 5.5 new feature</title>
- <metacharset="utf-8">
- <metahttp-equiv="X-UA-Compatible"content="IE=edge">
- <metaname="viewport"content="width=device-width, initial-scale=1">
- </head>
- <body>
- @env('local')
- You are in the local environment..
- @endenv
- </body>
- </html>
<!DOCTYPE html> <html> <head> <title>Laravel 5.5 new feature</title> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> @env('local') You are in the local environment.. @endenv </body> </html>
There are also some other example to understand the concept of Blade::if directive.
Let’s have a another example to check if user is subscribed or not.
- Blade::if('is_subscribed',function(){
- returnauth()->check()&&auth()->user()->isSubscribed();
- });
Blade::if('is_subscribed', function () { return auth()->check() && auth()->user()->isSubscribed(); });
Use this new directive in template :
- @is_subscribed
- User is Subscribed.
- @else
- User is not Subscribed.
- @endis_subscribed
@is_subscribed User is Subscribed. @else User is not Subscribed. @endis_subscribed
Hope this code and post will helped you for implement Laravel 5.5 new feature – Custom Blade::if Directives with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs