Search code examples
phplaraveleloquent-relationshiplaravel-filament

Laravel - Filament: get distinct values of a relationship column


I am using the Form Builder of Laravel Filament 3.x where I want to select only clients who have distinct first names.

I tried this but it displays all the clients, including those who have the same first_name:

 Select::make('client_id')
      ->relationship('client', 'first_name', 
           fn (Builder $query) => $query->distinct()),

Any hints on how to achieve the goal ?

Here is the Form Builder reference.

Here is the Elquent Builder.


Solution

  • You can try groupBy that gives you same result with distinct while using query builder

    Select::make('client_id')
      ->relationship('client', 'first_name', 
           fn (Builder $query) => $query->groupBy('first_name')),