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:
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:
I would want:
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:
But this is not working and the output is still just the name. The part " - my custom text" is missing.Solution:Jump to solution
maybe something like this.
```php
Select::make('user_id')
->label('Warrior')
->required()...
7 Replies
I think an accessor would be better
https://laravel.com/docs/10.x/eloquent-mutators#accessors-and-mutators
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Solution
maybe something like this.
If its a BelongsTo relationship can't you use
relationship('author', 'name')
in combination with getOptionLabelFromRecordUsing
?Yes, but I didn’t see a relationship in the code provided. 🙂
True! But if @Wirkhof didn't know you can also use it that way.
Thank you, guys. The mapWithKeys exampl seems to be working.
little late to the party, is it possible to append at the beginning of label instead? have u tried that?