F
Filament16mo ago
Arko

Default option with `->getSearchResultsUsing`

I've got a select like this:
Forms\Components\Select::make('project_id')
->label('Project')
->preload()
->searchable()
->getSearchResultsUsing(fn(string $searchQuery) => \App\Models\Project::query()
->where('name', 'like', "%{$searchQuery}%")
->pluck('name', 'id')
->toArray())
->getOptionLabelUsing(fn($project) => $project->name)
Forms\Components\Select::make('project_id')
->label('Project')
->preload()
->searchable()
->getSearchResultsUsing(fn(string $searchQuery) => \App\Models\Project::query()
->where('name', 'like', "%{$searchQuery}%")
->pluck('name', 'id')
->toArray())
->getOptionLabelUsing(fn($project) => $project->name)
But would like to preselect a project if its available in the route parameter, how can I do that? Tried adding ->default(1) (1 is a project ID) but that throws An attempt was made to evaluate a closure for [Filament\Forms\Components\Select], but [$project] was unresolvable. Also the ->preload() doesn't seem to do anything.
Solution:
Throws the same error as mentioned in my first post
Jump to solution
3 Replies
toeknee
toeknee16mo ago
It's a select so you would neeD: ->default([1]) But it will only work on the creation and not the edit view.
Solution
Arko
Arko16mo ago
Throws the same error as mentioned in my first post
Arko
ArkoOP16mo ago
->default([1]) does work on a select that loads a relationship, though. e.g.
Forms\Components\Select::make('assignees')
->multiple()
->relationship('users', 'name')
->default([1])
->preload(),
Forms\Components\Select::make('assignees')
->multiple()
->relationship('users', 'name')
->default([1])
->preload(),
Also the [] in the ->default() is only for when its a ->multiple(), which mine isn't Figured it out, I was missing ->options(Project::all()->pluck('name', 'id'))
Want results from more Discord servers?
Add your server