F
Filament•3mo ago
nwalty

Best way to determine which resource a select component is currently being rendered on

What would be the best way to determine what resource a shared select field is currently on to gather the context and update the available options? I have tried using RelationManager $livewire->getOwnerRecord() but this throws an error if I access the Resource directly. I have also played a little bit with the Livewire component and form component instances but wasn't getting very far with that and wasn't entirely sure that was the best route to go with this. EntityRoleResource shares its form on SchoolResource->RolesRelationManager and ProgramResource->RolesRelationManager. EntityRoleResource has a Select::make(role_id) where the role options would be dependent on the entity context (ie School or Program when rendered in their respective relation managers). Edit: EntityRoleResource should have all available options when being worked with directly.
2 Replies
🤖transistor🤖
To access the owner record you need to inject the livewire instance like so:
->options(function (RelationManager $livewire): array {
return $livewire->getOwnerRecord()->stores()
->pluck('name', 'id')
->toArray();
}),
->options(function (RelationManager $livewire): array {
return $livewire->getOwnerRecord()->stores()
->pluck('name', 'id')
->toArray();
}),
https://filamentphp.com/docs/3.x/panels/resources/relation-managers#accessing-the-relationships-owner-record so, if I understand correctly, Select::make('role_id') options depend on whether it is loaded under SchoolResource or ProgramResource, right? I would test the owner record to see if it contains a value from the parent resource to determine which options to load. Does this make sense?
nwalty
nwalty•3mo ago
This does work yes but wasn't necessarily the goal. The issue is when using RelationManager, if you try to use the EntityRoleResource directly, the reference to RelationManager causes a fatal error. So the goal was to come up with a way without repeating code to provide the form with context of where it is being accessed and to adjust the available options accordingly. Now I could make an arguement that directly accessing EntityRoleResource isnt absolutely necessary.