Filament Select with Relationship: Custom Label for titleAttribute Options

In Filament, when using Select with relationships, you may want to customize the Label of the dropdown options.

Traditionally, this Select label is defined by the titleAttribute parameter:

Forms\Components\Select::make('car_id')
->relationship(
name: 'car',
titleAttribute: 'name'
),

But what if you want to show the car year in addition to the name, like this?

The solution is to provide your custom logic as a callback function, in a method called getOptionLabelFromRecordUsing().

use Illuminate\Database\Eloquent\Model;
 
// ...
 
Forms\Components\Select::make('car_id')
->relationship(
name: 'car',
titleAttribute: 'name'
)
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->name} ({$record->year})")

As you can see, then you may skip the parameter titleAttribute, as it's not being used anyway.

The solution is for generally any custom label you want to show in the options.

Notice: you may think that you can use Eloquent Accessors here, but, in my experience, they don't work and will show the error "column not found".


If you want more Filament examples, you can find more real-life projects on our FilamentExamples.com.

avatar

Ok - now how does one show a table with a sortable colum with this ?

avatar

Can you explain what you want to achieve here? This lesson is focused on a dropdown, so we are unsure on what you mean by sortable table column

avatar

it means you can modify how something appears in the table where the relationship name is shown. For example i had a select form that gives the users the option to check out system users based on their user role and i didn't want to show the actual relationship names as stored in the database because it wasn't very user friendly so i used ->getOptionLabelFromRecordUsing() to mask the relationship role names to a more user friendly name

avatar

I am developing a project using Laravel with the Filament package. I have already created a form called "Area," which includes tables for State, LGA, Zone, Enumeration Area, and Code. Current Task: I now need to create a new form called "Street Enumeration." This form should include dependent select fields that allow users to choose: State LGA Zone Enumeration Area Code These selections should be populated based on existing records from the "Area" table. Additional Fields: In addition to the dependent selects, the form should also include the following fields: Street Name Entry Point (with longitude and latitude) Exit Point (with longitude and latitude) Street Description Request for Guidance: Could you provide guidance on how to implement this dependent select functionality and the additional fields in the "Street Enumeration" form? Thank you!

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 68 courses (1183 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses