F
Filament6mo 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
Matthew6mo 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
AxlF6mo 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.