Laravel 5.5 new feature - Custom Blade::if Directives with example

Laravel 5.5 new feature - Custom Blade::if Directives with example

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.


Here in this class, We will write a logic to check the apps environment inside the boot method :

  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\Blade;
  5. class AppServiceProvider extends ServiceProvider
  6. {
  7. /**
  8. * Bootstrap any application services.
  9. *
  10. * @return void
  11. */
  12. public function boot()
  13. {
  14. Blade::if('env', function ($env) {
  15. return app()->environment($env);
  16. });
  17. }
  18. /**
  19. * Register any application services.
  20. *
  21. * @return void
  22. */
  23. public function register()
  24. {
  25. }
  26. }
Welcome view

Now open your welcome.blade.php file to use your new blade directive in following way :

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Laravel 5.5 new feature</title>
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. </head>
  9. <body>
  10. @env('local')
  11. You are in the local environment..
  12. @endenv
  13. </body>
  14. </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.

  1. \Blade::if('is_subscribed', function () {
  2. return auth()->check() && auth()->user()->isSubscribed();
  3. });

Use this new directive in template :

  1. @is_subscribed
  2. User is Subscribed.
  3. @else
  4. User is not Subscribed.
  5. @endis_subscribed

Phone: (+91) 8800417876
Noida, 201301
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort