F
Filament12mo ago
Baspa

How to populate multi select list with default selected values

I have this multiple select form field where I load the options and try to set the default values:
Forms\Components\Select::make('platforms')
->options(function () {
return Platform::query()
->get()
->mapWithKeys(fn ($platform) => [$platform->id => $platform->name]);
})
->label(__('Platforms'))
->default(function () {
return ChannelPlatformFrequency::query()
->where('channel_id', request()->route('record'))
->get()
->mapWithKeys(fn ($channelPlatformFrequency) => [$channelPlatformFrequency->platform_id => $channelPlatformFrequency->platform->name]);
})
->multiple()
->required(),
Forms\Components\Select::make('platforms')
->options(function () {
return Platform::query()
->get()
->mapWithKeys(fn ($platform) => [$platform->id => $platform->name]);
})
->label(__('Platforms'))
->default(function () {
return ChannelPlatformFrequency::query()
->where('channel_id', request()->route('record'))
->get()
->mapWithKeys(fn ($channelPlatformFrequency) => [$channelPlatformFrequency->platform_id => $channelPlatformFrequency->platform->name]);
})
->multiple()
->required(),
The default method does not seem to work. It does not show any selected options. As far as I know I should use the default option to set the default selected values. As you can see in the image I attached the available options (up) and the default values (down) should match.
6 Replies
Dennis Koch
Dennis Koch12mo ago
Is this on Edit or Create? default() is for Create only
Baspa
BaspaOP12mo ago
On the edit page Hmmm ok, which one should I use on the edit page?
Dennis Koch
Dennis Koch12mo ago
Since your model already was saved there is no default anymore. You can use mutateDataBeforeFill to fill the form with default data
Baspa
BaspaOP12mo ago
Will try that! Thanks
Baspa
BaspaOP12mo ago
https://filamentphp.com/docs/3.x/actions/prebuilt-actions/edit#customizing-data-before-filling-the-form Is this the documentation about the method you mentioned? I tried to do so using this code, but it seems like mutateRecordDataUsing is not triggered:
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->label(__('Name'))
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('created_at')
->label(__('Created at'))
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->label(__('Updated at'))
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make()
->mutateRecordDataUsing(function (array $data): array {

$data['platforms'] = ChannelPlatformFrequency::query()
->where('channel_id', request()->route('record'))
->get()
->mapWithKeys(fn ($channelPlatformFrequency) => [$channelPlatformFrequency->platform_id => $channelPlatformFrequency->platform->name])
->toArray();

return $data;
})
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->label(__('Name'))
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('created_at')
->label(__('Created at'))
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->label(__('Updated at'))
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make()
->mutateRecordDataUsing(function (array $data): array {

$data['platforms'] = ChannelPlatformFrequency::query()
->where('channel_id', request()->route('record'))
->get()
->mapWithKeys(fn ($channelPlatformFrequency) => [$channelPlatformFrequency->platform_id => $channelPlatformFrequency->platform->name])
->toArray();

return $data;
})
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
Baspa
BaspaOP12mo ago
Ah I found it, nvm thanks 😄
Want results from more Discord servers?
Add your server