Β© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filamentβ€’2y agoβ€’
2 replies
Alexandre

Is it possible to format a new option created in a Select?

Hello all,
In my form, I have a select field that will display a list of buildings where the option is formatted in a certain way:
<strong>Building name</strong> - number, street | postal_code City (country)
<strong>Building name</strong> - number, street | postal_code City (country)

I use a method to get the buildings and populate my options :

$user = auth()->user();
     if ($user->syndicate) {
          $buildings = $user->syndicate->buildings()
                ->where('is_validated', true)
               ->get(['id', 'name', 'number', 'street', 'postal_code', 'city', 'country'])
               ->mapWithKeys(function ($building) {
                    $address = "<strong>{$building->name}</strong> - {$building->number}, {$building->street} | {$building->postal_code} {$building->city} ({$building->country})";
                    return [$building->id => $address];
               })
               ->toArray();
        } else {
            $buildings = [];
        }
$user = auth()->user();
     if ($user->syndicate) {
          $buildings = $user->syndicate->buildings()
                ->where('is_validated', true)
               ->get(['id', 'name', 'number', 'street', 'postal_code', 'city', 'country'])
               ->mapWithKeys(function ($building) {
                    $address = "<strong>{$building->name}</strong> - {$building->number}, {$building->street} | {$building->postal_code} {$building->city} ({$building->country})";
                    return [$building->id => $address];
               })
               ->toArray();
        } else {
            $buildings = [];
        }


I use createOptionForm() to give users the option of adding a new building to the Select and, in accordance with the documentation, I use createOptionUsing to create my building in the DB :

->createOptionUsing(function (array $data): int {
     return auth()->user()->syndicate->buildings()->create($data)->getKey();
}),
->createOptionUsing(function (array $data): int {
     return auth()->user()->syndicate->buildings()->create($data)->getKey();
}),


Everything works except that my select only displays the building ID in my select. Is it possible to format it too, like the others? If so, how?
Thanks in advance for your help πŸ˜‡
Solution
Hello all,
I finally figured out how to do it after a lot of research πŸ˜…

So, for my case I can use the getOptionLabelUsing method

->getOptionLabelUsing(function ($value) {
    $building = Building::find($value);

    if ($building) {
        return "<strong>{$building->name}</strong> - {$building->number}, {$building->street} | {$building->postal_code} {$building->city} ({$building->country})";
    }

    return $value;
    
})
->getOptionLabelUsing(function ($value) {
    $building = Building::find($value);

    if ($building) {
        return "<strong>{$building->name}</strong> - {$building->number}, {$building->street} | {$building->postal_code} {$building->city} ({$building->country})";
    }

    return $value;
    
})


$value
$value
is the value of the selected option (= the ID model)
And there is it πŸ₯³
Select - Form Builder - Filament
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel β€’ Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Is it possible to use "create new option in modal" feature with polymorphic select?
FilamentFFilament / β“β”Šhelp
13mo ago
Creatinga a new option in select
FilamentFFilament / β“β”Šhelp
16mo ago
Prevent select option re-rendering in SelectInput
FilamentFFilament / β“β”Šhelp
3y ago