morph to many select

how to do multi select for morph to many relation like morph to select
2 Replies
Hoceyne
HoceyneOP6d ago
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);
Want results from more Discord servers?
Add your server