Laravel @includeWhen Directive

We use this directive to call view based on condition. Normally, we can do something like this without using the directive.


However, Blade gives us a directive called @includeWhen. That's exactly what works and you're writing cleaner code.

@includeWhen($post->hasComments(), 'posts.comments');

You can also use the 3rd parameter to send data to the view.

@includeWhen($post->hasComments(), 'posts.comments', ['id' => 5]);

There are no comments, make the firs comment