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

AscentCreative's avatar

Blade Component attribute setter methods

I would love to have the ability to call a setter method when using a Blade Component, rather than forcing the parameters to be in the constructor of the class.

For example:

<x-foo-component  bash="boo"  xyz="abc" />

currently (as I understand it) needs:

use Illuminate\View\Component;

class Foo extends Component {
			public function __construct( $bash, $xyz ) {
						...
			}
}

However, what would be awesome would be to be able to do:

use Illuminate\View\Component;

class Foo extends Component {
			public function __construct( $bash ) {
						...
			}

			public function setXyz($val) {
					... setter code ...
			}
}

where setXyz() gets called as the component is set up.

On the face of it, this does the same thing, but I have a suite of components with various shared features and I'd love to work better with Traits and inheritance. The insistence on constructor params is unwieldy and leads to a lot of code duplication.

For example of a possible use case, a subset of my components might all use a facility whereby they're read-only to certain people (based on roles or some other logic). I would like to be able to easily add that to any component, so some form of AccessControl Trait would be ideal.

If I then set a readonly="{{ yes|no }}" on the blade tag, the trait setter function would get called and I wouldn't need to have added a new constructor param or duplicated the code into the component class. It would all be nicely encapsulated in the Trait. Inheritance would be too rigid as multiple features may need to flow down.

I've been digging into the Service Container binding stuff, and I can't see how to make this work. It's almost like, in the build() method after it's resolved the constructor params, it could look for setter methods for any parameters it didn't use... maybe that's too simplistic...

Or, is there a way to manually register the setters as something the Container would call while resolving the component?

Or another approach that I'm missing?

0 likes
9 replies
vincent15000's avatar

And what about something like this ?

class Foo extends Component {
	public $bash;
	public function __construct( $bash, $xyz ) {
				$this->bash = $bash;
	}
}
AscentCreative's avatar

@vincent15000 That's exactly what I do right now :)

The thing I'm wanting to avoid is the need to have a constructor parameter so that I can add features via traits where you can't overwrite the constructor.

1 like
AscentCreative's avatar

@vincent15000 So there's no way to tie into the Service Container registration? There's this tantalising comment in the docs:

https://laravel.com/docs/9.x/container

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.

I was hoping that meant what I was looking to do, but it's the only reference to setters in the whole page :)

1 like
AscentCreative's avatar

@vincent15000 Ah - it's ok - I posted a general update to the thread.

I was missing the fact that parameters not in the constructor get passed directly to the blade view in the $attributes variable, which means I can forget about needing to use the supporting class at all in many cases.

Snapey's avatar

You can't boot the trait from the constructor?

1 like
AscentCreative's avatar

You possibly can (althought I thought that was just for Eloquent), but the issue is getting the Component to actually call the methods and pass in the variables - that's the bit I'm digging into.

1 like
AscentCreative's avatar
AscentCreative
OP
Best Answer
Level 1

Never mind - after a load of digging, I've found the 'withAttributes' call which passes any variables not in the constructor into the view in the $attributes array.

That might solve most of what I was needing - it gives me access to the variables in the blade without needing massive constructors.

1 like

Please or to participate in this conversation.