Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use the $attributes variable to pass dynamic attributes to a Blade component


Can I use the $attributes variable to pass dynamic attributes to a Blade component


Yes, you can use the `$attributes` variable to pass dynamic attributes to a Laravel Blade component. Here's how:

1. In your component's Blade template, use the `$attributes` variable to render the attributes on the root element:

html
<button {{ $attributes }}>
    {{ $slot }}
</button>

2. In your parent view, pass the attributes to the component using the `:` prefix to evaluate the attribute value as a PHP expression:

html
<x-button :class="['btn', 'btn-primary']" :disabled="$isDisabled" :data-id="$item->id">
    Click me
</x-button>

3. The `$attributes` variable in the component's template will now contain the `class`, `disabled`, and `data-id` attributes, which will be rendered on the `