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
WirkhofOP9mo 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 Koch9mo 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
WirkhofOP9mo 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 Koch9mo ago
Use new HtmlString(). And probably also ->allowHtml() on the select
Wirkhof
WirkhofOP9mo ago
GREAT! Thanks! This: return new HtmlString($item['name'] . '<b>test</b'); is working.
Want results from more Discord servers?
Add your server