F
Filamentβ€’7mo ago
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) 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 ...
Jump to solution
1 Reply
Solution
Alexandre
Alexandreβ€’7mo ago
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 is the value of the selected option (= the ID model) And there is it πŸ₯³
Want results from more Discord servers?
Add your server