Filament Action: Select field shows ID instead of name when filling form

Hi everyone, I have a Filament Action that opens a form with a Select field for choosing an instructor. The issue is: When the action is opened, the instructor select field shows the instructor’s ID instead of their name. Here’s the situation: My fillForm() method sets the value using the instructor’s id. The select displays the raw id in the input, not the instructor name. If I change fillForm() to use the instructor name, it shows correctly, but then I get a validation or saving error saying it “only accepts IDs.” What’s the correct way to make the select field display the instructor’s name but still store the ID when submitting?
Action::make('Edit Section')
->modalWidth('md')
->fillForm(fn(Section $record) => [
'name' => $record->name,
'instructor_id' => $record->instructor_id,
'course_id' => $record->course_
])
->schema([
Select::make('instructor_id')
->label('Instructor')
->relationship(
'instructor',
modifyQueryUsing: fn($query) => $query->whereInstructorDepartment(auth()->user()->instructor- >department_id))
->getOptionLabelFromRecordUsing(fn($record) => $record->full_name)
->searchable()
->preload()
->required(),
Select::make('course_id')
->label('Course')
->options(
fn() => Course::whereDepartment(auth()->user()->instructor->department_id)
->pluck('code', 'id')
->toArray()
)
->searchable()
->preload()
->required(),
])
->action(function (array $data, Section $record) {
$record->update($data);
Notification::make()
->title('Section updated successfully')
->success()
->send();
})
Action::make('Edit Section')
->modalWidth('md')
->fillForm(fn(Section $record) => [
'name' => $record->name,
'instructor_id' => $record->instructor_id,
'course_id' => $record->course_
])
->schema([
Select::make('instructor_id')
->label('Instructor')
->relationship(
'instructor',
modifyQueryUsing: fn($query) => $query->whereInstructorDepartment(auth()->user()->instructor- >department_id))
->getOptionLabelFromRecordUsing(fn($record) => $record->full_name)
->searchable()
->preload()
->required(),
Select::make('course_id')
->label('Course')
->options(
fn() => Course::whereDepartment(auth()->user()->instructor->department_id)
->pluck('code', 'id')
->toArray()
)
->searchable()
->preload()
->required(),
])
->action(function (array $data, Section $record) {
$record->update($data);
Notification::make()
->title('Section updated successfully')
->success()
->send();
})
3 Replies
Adrian A
Adrian AOP2mo ago
No description
tuseto
tuseto2mo ago
Maybe something similar to ->relationship('month', 'name') ? month - relation name - which field to be shown on dropdown
Dennis Koch
Dennis Koch2mo ago
Does that prefilled instructor actually exist in your dataset? Or does your modifyQueryUsing maybe remove it from the dataset?

Did you find this page helpful?