Absolutely, this is possible and a great use case for customizing select options in Filament’s relation managers.
Goal:
When creating or editing a ContractRoyaltyDefinition via the royaltyDefinitions relation manager, restrict the selectable accounts to those linked to the parent Contract (from its BelongsToMany Accounts relationship).
Solution
In your relation manager’s form schema, for the account select field, use the options() method and pass a closure. This closure receives the $livewire component (the relation manager), allowing access to the parent Contract model via $livewire->ownerRecord (or $livewire->getOwnerRecord() depending on your Filament version).
Here’s a typical implementation:
use Filament\Forms;
public static function form(Forms\Form $form): Forms\Form
{
return $form
->schema([
Forms\Components\Select::make('account_id')
->label('Account')
// Only fetch accounts related to the parent contract
->options(function ($get, $set, $state, $livewire) {
$contract = $livewire->ownerRecord; // Or use getOwnerRecord() depending on version
// Check if the contract is loaded
if (!$contract) {
return [];
}
// Fetch related accounts
return $contract->accounts()
->pluck('name', 'id') // Adjust attribute as needed
->toArray();
})
->searchable()
->required(),
// ... other fields
]);
}
Or, depending on your Filament version and how you access the parent model:
->options(fn ($livewire) => $livewire->ownerRecord
? $livewire->ownerRecord->accounts()->pluck('name', 'id')->toArray()
: []
)
Notes
- Adjust
"name"if yourAccountmodel uses a different field. - This ensures only accounts attached to the contract are selectable.
- The key is leveraging
$livewire->ownerRecord, which is the contract being managed in the relation manager context.
Summary:
Yes, it’s possible and the above approach is the standard pattern in Filament for customizing select options based on the parent model context!