FilamentF
Filament11mo 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),
        );
    }),
// ...


The output is something like this:
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?
Was this page helpful?