Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

juanborras's avatar

Filament Form Select, add extra option

I've got a form with a relationship to load all the options (preload). I want to add an extra option "All the group" at the beginning. How could I add this extra option to the Select?

Thanks!

0 likes
2 replies
LaryAI's avatar
Level 58

To add an extra option like "All the group" to a Filament form select field that is populated with a relationship, you can use the options() method to prepend your custom option. Here's how you can achieve this:

Assuming you have a Filament form with a select field that loads options from a relationship, you can modify the select field like this:

use Filament\Forms\Components\Select;

Select::make('your_field_name')
    ->label('Your Field Label')
    ->options(function () {
        // Fetch the related options
        $relatedOptions = YourModel::all()->pluck('name', 'id')->toArray();

        // Prepend the custom option
        return ['all' => 'All the group'] + $relatedOptions;
    })
    ->searchable() // Optional: if you want the select to be searchable
    ->preload() // Optional: if you want to preload the options

In this example:

  • Replace YourModel with the model you are using for the relationship.
  • Replace 'name' and 'id' with the appropriate fields from your model that represent the display name and the value of the options.
  • The ['all' => 'All the group'] part adds the custom option at the beginning of the options list.

This approach allows you to add a custom option while still loading the rest of the options from the relationship.

1 like

Please or to participate in this conversation.