Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chiucs123-39881664's avatar

Slot attributes not working

Hi guys,

I hope this thread finds you well.

I was trying to make a component that generates locale-based inputs depending on the site configuration in my application, however I face difficulty when I was attempting to have the component generate multiple instances of the inputs by using the slots method.

To simplify lets say we have a x-fields component where it has the following content.

@php
$langs = ['en','jp'];
@endphp

<div class="container">
@foreach($langs as $lang)
{{ $slot->withAttributes(['lang' => $lang]) }}
@endforeach
</div>

And I was thinking of doing something like in page.blade.php:

<x-fields name="something">
    <x-input :$lang />
</x-fields>

Which ideally gives me:

<div class="container">
    <input name="something[en]" />
    <input name="something[jp]" />
</div>

From my observation it seems like the slots were rendered before the for-loop happens and the variable not found error takes place for $lang.

May I ask if there's a better way of doing this, ideally within the scope of blade templates?

Thank you and have a wonderful day!

1 like
3 replies
vincent15000's avatar

I don't see anywhere in the documentation that it's possible to write $slot->withAttributes.

Where did you see this ?

vincent15000's avatar

@ghabe

$slot->withAttributes(['lang' => $lang]) will replace all attributes with those you provide as parameters and returns the slot itself.

{{ $slot->withAttributes(['lang' => $lang]) }}

is equivalent to

{{ $slot }}

And as it replaces all attributes, after your loop, the attributes will be only the last lang attribute.

Please or to participate in this conversation.