F
Filament10mo ago
morty

BelongsToMany Select component doesn't populate data in a modal form

I have an action on a table that opens a modal form to manage a BelongsToMany (many-to-many) relationship. This is my action code on my table:
->actions([
Tables\Actions\Action::make('collections')
->label('Manage collections')
->icon('heroicon-o-rectangle-stack')
->slideOver()
->modalWidth(MaxWidth::Medium)
->modalIcon('heroicon-o-rectangle-stack')
->form([
Forms\Components\Select::make('collections')
->label('Collections')
->multiple()
->relationship('collections', 'name')
->options(AccountCollection::mine()->pluck('name', 'id'))
->required(),
]),
])
->actions([
Tables\Actions\Action::make('collections')
->label('Manage collections')
->icon('heroicon-o-rectangle-stack')
->slideOver()
->modalWidth(MaxWidth::Medium)
->modalIcon('heroicon-o-rectangle-stack')
->form([
Forms\Components\Select::make('collections')
->label('Collections')
->multiple()
->relationship('collections', 'name')
->options(AccountCollection::mine()->pluck('name', 'id'))
->required(),
]),
])
According to the documentation: https://filamentphp.com/docs/3.x/forms/fields/select#integrating-with-an-eloquent-relationship
Filament will load the options from the relationship, and save them back to the relationship's pivot table when the form is submitted.
Filament is saving no problem, but it's not loading the values already associated with the model. I tried using the ->fillForm() method but then the ->options() array is blank. I don't understand what I'm doing wrong here? In the screenshot, some of these options are already associated with this account but the form doesn't indicate this.
No description
Solution:
->fillForm(fn (Model $record) => $record->collections->toArray())
->fillForm(fn (Model $record) => $record->collections->toArray())
? options() isn't necessary I guess...
Jump to solution
2 Replies
Solution
LeandroFerreira
LeandroFerreira10mo ago
->fillForm(fn (Model $record) => $record->collections->toArray())
->fillForm(fn (Model $record) => $record->collections->toArray())
? options() isn't necessary I guess
morty
mortyOP10mo ago
I swear I tried this already... it's working though. Must have had a slight variation. Thanks!
Want results from more Discord servers?
Add your server