FilamentF
Filament11mo ago
Alisher

Select filter attribute name by relation

I have an form select like that
Forms\Components\Select::make('teachers')
  ->label('Teachers')
  ->relationship('teachers')
  ->multiple()
  ->preload()
  ->searchable()
  ->required()
  ->options(
      Teacher::with('user')->get()->pluck('user.name', 'id')->toArray()
  )

So Teacher has relation user and I need to get label from user.name

sho I wrote filter like that
Tables\Filters\SelectFilter::make('teachers')
    ->label('Filter by Teacher')
    ->relationship('teachers', 'user.name')
    ->multiple()
    ->preload()
    ->options(fn () => Teacher::with('user')->get()->pluck('user.name', 'id')->toArray())


for SelectFilter relationship 2nd param is required and if is not working with relations.
SQLSTATE[HY000]: General error: 1 no such column: user.name (Connection: sqlite, SQL: select distinct "teachers".* from "teachers" left join "speaking_club_teacher" on "teachers"."id" = "speaking_club_teacher"."teacher_id" order by "user"."name" asc)
Was this page helpful?