Hoceyne
Hoceyne
FFilament
Created by Hoceyne on 12/8/2024 in #❓┊help
morph to many select
Hello, thank you for your response, actually that's not my case, I ended up building custom select with custom create:
Forms\Components\Select::make('unit_id')
->label(__('Unit'))
->options(fn() => User::find(Filament::auth()->user()->id)->country->units->pluck('name', 'id')->toArray())
->searchable()
->afterStateHydrated(function ($record, $component, $state) {
$options = $component->getOptions();
if (count($options) === 1) {
$component->state(array_key_first($options));
$component->disabled(true);
} elseif ($state === null) {
$component->state($record?->unit->first()?->id);
}
}),
Forms\Components\Select::make('unit_id')
->label(__('Unit'))
->options(fn() => User::find(Filament::auth()->user()->id)->country->units->pluck('name', 'id')->toArray())
->searchable()
->afterStateHydrated(function ($record, $component, $state) {
$options = $component->getOptions();
if (count($options) === 1) {
$component->state(array_key_first($options));
$component->disabled(true);
} elseif ($state === null) {
$component->state($record?->unit->first()?->id);
}
}),
and this is the custom create function the save morph to many relation
$unit = Unit::findOrFail($data['unit_id']);
$record->unit()->save($unit);
$unit = Unit::findOrFail($data['unit_id']);
$record->unit()->save($unit);
3 replies