oliverbj
oliverbj

Reputation: 6052

PHP/Laravel - Chartjs implode does not function

I am trying to use Chartjs with PHP and show a chart on my website. To do that, I get the "labels" from my database and add it to an array like this:

ChartController.php:

for($i = 0; $i < 5; $i++){
    $labels[] = Carbon::now()->startOfWeek()->addDay($i)->format("d M");
}

Above gives me below array, which I need to use as my labels:

array:5 [▼
  0 => "16 Jul"
  1 => "17 Jul"
  2 => "18 Jul"
  3 => "19 Jul"
  4 => "20 Jul"
]

Now, ChartJS labels needs to be formatted like this:

labels(['16 Jul', '17 Jul', '18 Jul', '19 Jul', '20 Jul'])

To do that, I use the implode() function like this:

labels("['".implode('\',\'', $labels). "']")

Which gives me this output:

['16 Jul','17 Jul','18 Jul','19 Jul','20 Jul']

However, this give me the following error:

Argument 1 passed to Fx3costa\LaravelChartJs\Builder::labels() must be of the type array, string given, called in /srv/users/serverpilot/apps/milestonechecker/app/Http/Controllers/ChartController.php on line 42

Upvotes: 0

Views: 191

Answers (1)

Vasyl Zhuryk
Vasyl Zhuryk

Reputation: 1248

You tried to do like this: labels([String]), but you need to pass array as label. Please try this:

labels($labels);

Upvotes: 1

Related Questions