[Chartjs]-Inserting data from controller to an array in javascript

2👍

You can create two array in your controller.

$label = [];
$dataset = [];

and loop through your collection and push data to these arrays

foreach($data as $value){
   $label[] = $value->data_field;
   $dataset[] = $value->data_field;
}

And pass it to your blade and assign these array to your chart

...
var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: Bulan,
                datasets: [{
                    label: 'Nilai Pendapatan',// label data
                    data: Pendapatan, // dataset
                    borderWidth: 1
                }]
            },
....

Leave a comment