How to customize the label for options in CheckboxList?

Right now I am getting the options for ChecboxList like:
CheckboxList::make('cars')
->options(
Car::where('is_red', true)
->orderBy('year', 'desc')
->pluck('name', 'id')
)
CheckboxList::make('cars')
->options(
Car::where('is_red', true)
->orderBy('year', 'desc')
->pluck('name', 'id')
)
and it works. But I need to do custom made each option that can use HTML code, for example:
<span style="font-weight: bold;">name<span> - with ID: <span>id</span>
<span style="font-weight: bold;">name<span> - with ID: <span>id</span>
How to do that?
Solution:
Use new HtmlString(). And probably also ->allowHtml() on the select
Jump to solution
5 Replies
Wirkhof
Wirkhof4mo ago
Can I somehow run/encapsulate this part:
Car::where('is_red', true)
->orderBy('year', 'desc')
->pluck('name', 'id')
Car::where('is_red', true)
->orderBy('year', 'desc')
->pluck('name', 'id')
via some map function or something before giving it to options()? Has anybody done that or something else that is going to give me the final altered html result for each label/option of the checklist? I am getting this error: array_map(): Argument #2 ($array) must be of type array, Illuminate\Support\Collection given when trying
->options(
array_map(
function ($u) {
$u['name'] = $u['name'] + '<b>test</b>';
return $u;
},
Car::where('is_red', true)
->orderBy('year', 'desc')
->pluck('name', 'id')
)
)
->options(
array_map(
function ($u) {
$u['name'] = $u['name'] + '<b>test</b>';
return $u;
},
Car::where('is_red', true)
->orderBy('year', 'desc')
->pluck('name', 'id')
)
)
Is there an alternative function itself to array_map that can mutate collection content, in my case the "name"? Perhaps some function that comes with Laravel itself? Hmm, perhaps I am doing it too complicated. Can I use map() directly on a model like:
->options(
Car::where('is_red', true)
->orderBy('year', 'desc')
->pluck('name', 'id')
->map(function (string $item) {
return $item['name'] . '<b>test</b';
})
)
->options(
Car::where('is_red', true)
->orderBy('year', 'desc')
->pluck('name', 'id')
->map(function (string $item) {
return $item['name'] . '<b>test</b';
})
)
When I tried this code, I got:
Cannot access offset of type string on string
Cannot access offset of type string on string
Ignition highlights this line:
return $item['name'] . '<b>test</b';
return $item['name'] . '<b>test</b';
Any idea how I can mutate existing name for the option label in the checkbox using map() or other method?
Dennis Koch
Dennis Koch4mo ago
When I tried this code, I got:
Yes. You used pluck. $item == the name of the car You could debug this, if you just dd()ed $item
Wirkhof
Wirkhof4mo ago
Thanks, it's working now, if I just do $item . '<b>test</b>' Although, it's outputed as a string Any idea how to make it render as html ? so the <b> tag will make the "test" bold?
Solution
Dennis Koch
Dennis Koch4mo ago
Use new HtmlString(). And probably also ->allowHtml() on the select
Wirkhof
Wirkhof4mo ago
GREAT! Thanks! This: return new HtmlString($item['name'] . '<b>test</b'); is working.
Want results from more Discord servers?
Add your server
More Posts
Repeater set a Variable per FieldHello There I'm creating a Form with a Repeater Element. For each Field in that Repeater I need to Is there a way to disable the tenant registration?I am currently using the tenant methods on the admin panel, tenancy is working. I have a custom `->Unable to use TinyEditor Media ModalI have the problem that I cannot use the media modals on a current Filament V2 project (e.g. the hreThe POST method is not supported for route admin/login. Supported methods: GET, HEAD.login request method is POST. how i can change it ? when i see list of routes admin/login method is Using tab in custom pageHow can I use filter tab in livewire component? I saw this converstaion but got an error. https://giIs it possible to use queryStringIdentifier() for widgets with simple pagination?I'm using the `queryStringIdentifier()` for 2 widget table to resolve the pagination issue, but whenHow To Get data from form in login page into resource page or for query buildercan anyone help for this form ? how can i get the year data from this form to implement in all of myEquivalent of UpdateOrCreateHello, I want the createAction to check if a record already exists and then update it instead of creDetect image in FileUpload to hide upload instructionsI'm having trouble with changing the avatar after I upload it using the FileUpload field. Here's whaLivewire function is being called after Table is renderedI have a action that calls a livewire function to add a filter to a db query. But the function call