F
Filament10mo ago
AxlF

Select first item

Hey, I would like to select the first option item, but somehow it isn't working as expected. UI wise, I get the result when I use ->selectablePlaceholder(false). But the form throws a validation required error on that field.
Forms\Components\Select::make('project_id')
->relationship(
name: 'project',
titleAttribute: 'title',
modifyQueryUsing: fn (Builder $query) => $query->whereHas('users', fn ($q) => $q->where('user_id', auth()->id()))->orderBy('title')
)
->label('Projekt')
->columnSpanFull()
->selectablePlaceholder(false)
->required(),
Forms\Components\Select::make('project_id')
->relationship(
name: 'project',
titleAttribute: 'title',
modifyQueryUsing: fn (Builder $query) => $query->whereHas('users', fn ($q) => $q->where('user_id', auth()->id()))->orderBy('title')
)
->label('Projekt')
->columnSpanFull()
->selectablePlaceholder(false)
->required(),
What am I doing wrong? Bye
2 Replies
Matthew
Matthew10mo ago
Maybe modifyQueryUsing: fn (Builder $query) => $query->whereHas('users', fn ($q) => $q->where('user_id', auth()->id()))->orderBy('title')->first()
Forms\Components\Select::make('project_id')
->relationship(
name: 'project',
titleAttribute: 'title',
modifyQueryUsing: fn (Builder $query) => $query
->whereHas('users', fn ($q) => $q->where('user_id', auth()->id()))
->orderBy('title')
)
->label('Projekt')
->columnSpanFull()
->selectablePlaceholder(false)
->required()
->default(function () {
return Project::query()
->whereHas('users', fn ($q) => $q->where('user_id', auth()->id()))
->orderBy('title')
->first()
?->id;
}),
Forms\Components\Select::make('project_id')
->relationship(
name: 'project',
titleAttribute: 'title',
modifyQueryUsing: fn (Builder $query) => $query
->whereHas('users', fn ($q) => $q->where('user_id', auth()->id()))
->orderBy('title')
)
->label('Projekt')
->columnSpanFull()
->selectablePlaceholder(false)
->required()
->default(function () {
return Project::query()
->whereHas('users', fn ($q) => $q->where('user_id', auth()->id()))
->orderBy('title')
->first()
?->id;
}),
AxlF
AxlFOP10mo ago
Thank you. Isn't it possible to get the options data, without doing a second query? I think they are already loaded and it should't be necessary to load it again.
Want results from more Discord servers?
Add your server