Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Named slots in Blade components in Laravel 7.x

The new Blade components in Laravel 7 are great. They provide an easy way of defining blade components in a Vue-like tag aliases. One of the nice features of Blade components are slots.

Basically, what slot allows you to do is inject dynamic content into the blade component. For instance, if we’ve this sidebar component…

<!-- /resources/views/components/sidebar.blade.php -->

<div class="collapse list-unstyled">
    <!-- want to fill in dynmic content here -->
</div>

And we want to inject some dynamic content inside the <div>, we can achieve this using slots like so.

<!-- /resources/views/components/sidebar.blade.php -->

<div class="collapse list-unstyled">
    {{ $slot }}
</div>

And now, we can pass content to the slot by injecting content into the component like so.

<x-sidebar>
    <ul>
        <li>Home</li>
        <li>Articles</li>
        <li>About</li>
    </ul>
</x-sidebar>

All the content inside the <x-sidebar> tags will get slotted in the $slot variable of the component where it is used.

Now, what if we want to use multiple slots inside of the component? Well, we can do this by using “Named” slots. And it seems like the inspiration is taken from the similar concept in Vue.js.

Named Slots

At its heart, if you want to use multiple slots inside of a component, you can use a <x-slot> tag and pass in a name attribute to it. For instance, in our previous example, if we want to insert a user avatar to the component, we can add a <x-slot> tag with attribute name="avatar" like so.

<x-sidebar>
    <x-slot name="avatar">
        <img src="/image/user-avatar.png" />
    </x-slot>

    <ul>
        <li>Home</li>
        <li>Articles</li>
        <li>About</li>
    </ul>
</x-sidebar>

…Which can be further accessible in the component as a $avatar variable like so.

<!-- /resources/views/components/sidebar.blade.php -->

<div>
    {{ $avatar }}
</div>

<div class="collapse list-unstyled">
    {{ $slot }}
</div>

This way you can add multiple slots into your component which can come handy in certain situations like the one I’ve described above.

Dynamic Slot Names

Now, apart from declaring slot with fixed names, you can also make the name for slots dynamic. For this, all you need to do is to prefix : before the name attribute and passing in the dynamic varibale like so.

<x-slot :name="$avatar">
    <img src="/image/user-avatar.png" />
</x-slot>

Here, the $avatar would be a variable that contains a string which would act as the name of the slot.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?