Laravel : Is it possible to use the Spread operator in a component constructor?

Viewed 249

I'm using the blade component system with Laravel 8.

I'm currently getting a lot of parameters to pass to a component. So I'm searching a way to pass parameters (or attributes) from the blade template into the component class constructor with the spread operator.

So :

<x-mycmp regularA="something" regularB="somethingelse" custom="test" />

To be understood by :

public function __construct ($regularA, $regularB, ...$addidionnalVars)
{
    // ...
}

Where the "custom" attribute will be in the $addidionnalVars array.

For now, I got a "laravel Unresolvable dependency resolving [Parameter #2 [ ...$addidionnalVars]] in class" error.

Is there a way to make this works ?

1 Answers

Update: Solution below only seems to work for Blade X

This used to work for BladeX, maybe it works for you too?

<x-mycmp regularA="something" regularB="somethingelse" ...$spreadVariables/>
Related