Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rafaeladi's avatar

laravel charts with query builder

Is there any documentation on how to use laravel charts with data from DB but the one with query builder not eloquent? I cant seem to find it anywhere and how the data being passed is kind of different so i need some help in this

0 likes
6 replies
fylzero's avatar

@rafaeladi I looked at the package briefly and it seems like this uses Chartisan under the hood. Which is basically a Library, called similarly to a facade. I'm not sure if that syntax is what is tripping you up or if you actually mean the data. You can populate the data using Eloquent or QueryBuilder.

public function handler(Request $request): Chartisan
{
    $data = DB::table('whatever')->pluck('column_with_numbers');

    return Chartisan::build()
        ->labels(['First', 'Second', 'Third'])
        ->dataset('Sample', $data->toArray());
}

Obviously keeping this example very simple... but you can grab labels dynamically if needed... pretty much do whatever you need to. Hope this helps.

rafaeladi's avatar

@fylzero I tried to gather the data from my db but everytime i genereate the chart it always show error

UNEXPECTED TOKEN < IN JSON AT POSITION 0

And this is the code that i had for now,

public function handler(Request $request): Chartisan
    {  
	$label = DB::table('test_suite')->pluck('name');
    $data=DB::table('test_execution')->pluck('duration');	

       return Chartisan::build()
        ->labels($label->toArray())
        ->dataset('Number of tests', [1, 2, 8,3,6,1, 2, 8,3,6])
        ->dataset('Errors made', $data->toArray());
        
    }
fylzero's avatar

@rafaeladi Try using static labels and a static data set. This should tell you if the problem is Laravel charts or something else.

Is there any type of stack trace telling you what is throwing that error?

rafaeladi's avatar

@fylzero if i did the whole thing with static data, it gives me the results that i want. I think part of the problem here is how the data is being structured when passed to the laravel charts.

I dont have a stack trace since the view can be rendered but only the charts doesnt, so the error message that i had only from the error that are displayed on the chart container

rafaeladi's avatar

@fylzero So i just figured out how to view the error

Call to undefined method Illuminate\Database\Query\Builder::toArray()

And i think this comes from this code

 $total_test=DB::table('test_executions as e')

                    ->where('e.case_id','1')

                    ->toArray();

i made that mistake and now replaced the 'toArray()' with 'get()' but the intended data is still not visible

fylzero's avatar
fylzero
Best Answer
Level 67

@rafaeladi get() will return a collection, so you'd need to turn that into an array.

$total_test = DB::table('test_executions as e')
    ->where('e.case_id','1')
	->get()
    ->toArray();

Please or to participate in this conversation.