Home » Php » php – Laravel Blade – Advantage of @slot/@component vs @include?

php – Laravel Blade – Advantage of @slot/@component vs @include?

Posted by: admin April 23, 2020 Leave a comment


Laravel 5.4 Blade introduced the concept of components & slots – but I can’t see what they add over the traditional @include. As I understand, with component/slots, you do:

In template component-tpl.blade.php:

<div class='container'>

Using slots in page template, you do:

    The content of Slot 1
    The content of Slot 2

What functionality does that provide over the older:

@include('component-tpl',['slot1'=>'The content of Slot 1',
'slot2'=>"The content of Slot 2"])

using the exact same ‘component-tpl.blade.php’ Blade template?

What am I missing? Thanks for any insights.


How to&Answers:

As stated, there’s no functional difference, but careful use of both can give you cleaner blade files.

If a slot could contain HTML, then using a component will give a cleaner syntax in your blade files.

   <strong>This text has html</strong>


@include('test', ['slot' => '<strong>This text has HTML</strong>'])

Equally, if a component has no slots, then an include may be preferred:





I think I’ve tracked down another crucial difference. For instance, from the documentation for 5.4:

Blade’s @include directive allows you to include a Blade view from within another view. All variables that are available to the parent view will be made available to the included view:

As far as I can tell, components have a different scope from a containing view and so the variables available to the parent view are not available within the component. You need to pass a variable to a component like this:

@component('alert', ['foo' => 'bar'])

This discussion is related to this problem:
Use variables inside the Markdown Mailables


As the documentation says:

Components and slots provide similar benefits to sections and
layouts; however, some may find the mental model of components and
slots easier to understand