F
Filament2mo ago
Suky

Dinamically update options based on another input

so the question is pretty straight forward, is there a way to update the options of a select field based on another field? so these options would be nice if I could add an where query
Forms\Components\Select::make('bundle_descriptor_id')
->label('Bundle Descriptor')
->searchable()
->relationship('descriptor', 'name')
->createOptionForm([
Forms\Components\TextInput::make('name')
->required(),
])
->options(function () {
return \App\Models\StreamingBundleDescriptor::all()->pluck('name', 'id')->toArray();
})
->required(),
Forms\Components\Select::make('bundle_descriptor_id')
->label('Bundle Descriptor')
->searchable()
->relationship('descriptor', 'name')
->createOptionForm([
Forms\Components\TextInput::make('name')
->required(),
])
->options(function () {
return \App\Models\StreamingBundleDescriptor::all()->pluck('name', 'id')->toArray();
})
->required(),
based on this input
Forms\Components\Select::make('streaming_bundle_id')
->label('Streaming Bundle')
->searchable()
->options(function () {
return \App\Models\StreamingBundle::all()->pluck('name', 'id')->toArray();
})
->required(),
Forms\Components\Select::make('streaming_bundle_id')
->label('Streaming Bundle')
->searchable()
->options(function () {
return \App\Models\StreamingBundle::all()->pluck('name', 'id')->toArray();
})
->required(),
Solution:
You need to use ->live() in such a case. Take a look in the docs to figure all related methods like ->afterStateUpdated() https://filamentphp.com/docs/3.x/forms/advanced...
Jump to solution
6 Replies
Suky
Suky2mo ago
bump
MohamedSabil83
MohamedSabil832mo ago
If you mean to filter the options of bundle_descriptor_id according to streaming_bundle_id value, then update options in the bundle_descriptor_id to be similar to this:
->options(function (Forms\Get $get) {
return \App\Models\StreamingBundleDescriptor::where('streaming_bundle_id', $get('streaming_bundle_id'))->pluck('name', 'id')->toArray();
})
->options(function (Forms\Get $get) {
return \App\Models\StreamingBundleDescriptor::where('streaming_bundle_id', $get('streaming_bundle_id'))->pluck('name', 'id')->toArray();
})
Suky
Suky2mo ago
gotcha so it's $get for things like these, tysm does it update in real time like it's wire:model.live? or do I have to set some parameters in order to do it also what if I got this
Forms\Components\Select::make('supplier_id')
->label('Supplier')
->required()
->searchable()
->options(
\App\Models\Supplier::all()
->pluck('name', 'id')
)
->columnSpan(2),
Forms\Components\TextInput::make('total_units')
->required()
->columnSpan(2),
Forms\Components\TextInput::make('cost_unit')
->required()
->columnSpan(2),
Forms\Components\TextInput::make('total_cost')
->required()
->columnSpan(2),
Forms\Components\Repeater::make('marketplaces')
->relationship('markets')
->reorderableWithButtons()
->schema([
Forms\Components\Select::make('marketplace_id')
->searchable()
->label('Marketplace')
->required()
->options(
\App\Models\Marketplace::all()
->pluck('country', 'id')->toArray()
)
->columnSpan(1),
Forms\Components\TextInput::make('quantity')
->required()
->columnSpan(1),
])
->columnSpan(4),
Forms\Components\Select::make('supplier_id')
->label('Supplier')
->required()
->searchable()
->options(
\App\Models\Supplier::all()
->pluck('name', 'id')
)
->columnSpan(2),
Forms\Components\TextInput::make('total_units')
->required()
->columnSpan(2),
Forms\Components\TextInput::make('cost_unit')
->required()
->columnSpan(2),
Forms\Components\TextInput::make('total_cost')
->required()
->columnSpan(2),
Forms\Components\Repeater::make('marketplaces')
->relationship('markets')
->reorderableWithButtons()
->schema([
Forms\Components\Select::make('marketplace_id')
->searchable()
->label('Marketplace')
->required()
->options(
\App\Models\Marketplace::all()
->pluck('country', 'id')->toArray()
)
->columnSpan(1),
Forms\Components\TextInput::make('quantity')
->required()
->columnSpan(1),
])
->columnSpan(4),
and I want the sum of the quantity from the marketplaces to be auto updated inside the total_units input?
Solution
MohamedSabil83
MohamedSabil832mo ago
You need to use ->live() in such a case. Take a look in the docs to figure all related methods like ->afterStateUpdated() https://filamentphp.com/docs/3.x/forms/advanced
Suky
Suky2mo ago
thanks ur an mvp
MohamedSabil83
MohamedSabil832mo ago
You're welcome. Glad to help.