Laravel Include When Directive

Laravel Include When Directive

A new feature in Laravel Blade is an includeWhen directive. This allows you to simplify a typical if statement into a single line.

“I think it’s a cool feature as it tidies up so much boilerplate”, said James Brooks, the author of the pull request.

To see this feature in use pretend you have this common setup:

@if(Auth::user())
    @include('nav.user')
@endif

Now, this can be simplified using includeWhen:

@includeWhen(Auth::user(), 'nav.user')

Or as another example for those using Laravel’s Authorization system:

@if ($user->ownsPost($post)
     @include('posts.edit-controls', ['post' => $post])
@endif

Can be changed to the following:

@includeWhen($user->ownsPost($post), 'posts.edit-controls', ['post' => $post])

This feature is now included in Laravel and you can run a composer update to be sure you are on the latest release. For more Blade features check out the Laravel Blade category here on Laravel News.

Filed in: News / Blade
Laravel News Partners

Laravel Jobs

Senior Full-Time Laravel Developer (REMOTE)
Remote, ANYWHERE
Vue School
Senior Laravel Developer
Remote, USA & Canada Only
Givecloud
Full Stack Laravel Developer (Full-time/Contract)
Remote or Columbus, OH
EduSourced
Backend PHP Developer
Hamburg
ABOUT YOU GmbH
Senior Developer
Remote
Shelterluv

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.