F
Filamentβ€’4d ago
Lukas

Inconsistent behaviour in relation manager dependency injection

Hi, why is it that if I use dependency injection in a relationship manager table row, sometimes the model is loaded with the related relationship records and sometimes not? For example, in a scenario where Foo has a one-to-many relationship with Bar via the bars() relationship. Inside the BarsRelationManager of the FooResource I have the following code:
// ...
Tables\Columns\ToggleColumn::make('baz')
->updateStateUsing(static function (Bar $record, Foo $ownerRecord, Foo $owner, $livewire) {
// Attempt to load the bars relationship
$owner = $owner->load(['bars']);


$ownerFromLivewire = $livewire->getOwnerRecord();


dd(
sizeof($owner->bars),
sizeof($ownerRecord->bars),
sizeof($ownerFromLivewire->bars),
);
}),
// ...
// ...
Tables\Columns\ToggleColumn::make('baz')
->updateStateUsing(static function (Bar $record, Foo $ownerRecord, Foo $owner, $livewire) {
// Attempt to load the bars relationship
$owner = $owner->load(['bars']);


$ownerFromLivewire = $livewire->getOwnerRecord();


dd(
sizeof($owner->bars),
sizeof($ownerRecord->bars),
sizeof($ownerFromLivewire->bars),
);
}),
// ...
The output is something like this:
0
0
3
0
0
3
How come that $ownerFromLivewire which uses the $livewire property to get the owner record successfully retrieves the relation records, while $owner which explicitly loads the relationship and $ownerRecord which does not both do not retrieve the relation records? I tested it with and without Model::preventLazyLoading() and both return the same results. Am I missing something?
4 Replies
Dennis Koch
Dennis Kochβ€’3d ago
Is $owner even the correct model? Same ID? Or did it just inject an empty model?
Lukas
LukasOPβ€’3d ago
Ah, lol yeah. I shouldn't program that late in the night πŸ˜„ Thanks πŸ˜„
Dan Harrin
Dan Harrinβ€’2d ago
in v4 models incorrectly injected will throw an exception
Lukas
LukasOPβ€’2d ago
Very nice, this has bitten me than once already πŸ˜„
Although it would be nice if the owner record could also be injected where applicable instead of having to go through the livewire component.

Did you find this page helpful?