Modifying the option label in Select

I would like to modify the option's label in the select box.

My working code without any added text looks like this:
->schema([
    Select::make('user_id')
        ->label('Warrior')
        ->required()
        ->searchable()
        ->options(User::all()->pluck('name', 'id')
])


And this works and the user names are shown based on what you type in the select box.

I would like to add a custom text (I will replace it later with a custom generated quote like "I only trust my sword" etc.)
at the end of the name.

So, instead of what I have now:

Jar Grimsh
Witek Boiar
Zilgert Brimbright


I would want:

Jar Grimsh - I only trust my sword.
Witek Boiar - War, war, and war!
Zilgert Brimbright - Light over darkness.


In the results in the select box as the user is typing stuff in it.

How to alter the option output to reflect the added text?

Let's say the labels are stored in the table called quotes and they have an id and quote for the quote itself. And it should be random.

Or just with a custom static text or html like - my custom quote or <b style='color: green;'> - my custom quote</b>

I have tried suggestions my some Discord users like:

->schema([
    Select::make('user_id')
        ->label('Warrior')
        ->required()
        ->searchable()
        ->options(User::all()->pluck('name', 'id')
        ->getOptionLabelFromRecordUsing(fn(Model $record) => "{$record->name} - my custom text")
])

But this is not working and the output is still just the name. The part " - my custom text" is missing.
Solution
maybe something like this.
Select::make('user_id')
  ->label('Warrior')
  ->required()
  ->searchable()
  ->options(function(): array {
      return User::all()->mapWithKeys(function ($user) {
          return [$user->id => $user->name . ' - ' . $user->quote];
      })->toArray();
  })
Was this page helpful?