earmsby's avatar

custom Select options

I have a model and filament resource called Contract. It has a HasMany relationship called royaltyDefinitions to the ContractRoyaltyDefinition model.

The Contract model has a BelongsToMany relationship with Accounts. The ContractRoyaltyDefinition model has a BelongsTo relationship with Accounts.

In my relation manager on the Contract resource for the royaltyDefinitions relationship, I have a select to select the Account ID for the ContractRoyaltyDefinition. I'd like to customize the list of accounts so that, rather than showing all the accounts in the table, it only shows accounts in the BelongsToMany relationship with the Contract being viewed.

Is this possible? It seems like it should be but I'm sure of the right approach.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

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 your Account model 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!

Please or to participate in this conversation.