简体   繁体   中英

Passing Labels from PHP to Chart.js

I'm trying to pass a list of names into the labels section of a Chart.js chart which I'm retrieving from a database with PHP.

In my controller I do

json_encode(array_pluck($var->toArray(), 'name'));

which returns:

"["name1","name2","name3","name4","name5","name6"]"

In the view, I have:

labels: {{ $var }},

and when I view the source, I can see

"name1","name2","name3","name4","name5","name6"

This is causing the chart to fail as it's expecting:

'name1','name2','name3','name4','name5','name6'

I've tried various things such as html_entity_decode , json_decode and htmlspecialchars_decode but not of them seem to do anything.

You have to use {!! $assets_by_department_departments !!} {!! $assets_by_department_departments !!} - This will prevent Blade from escaping the data.

See https://laravel.com/docs/5.6/blade#displaying-data for further details :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM