[Chartjs]-ChartJS and Laravel5

0đź‘Ť

In your controller you call return view('admin.main')->with('userInfo',$user_info);

with('userInfo',$user_info) sets a $userInfo variable in your view with the content of $user_info, so just something like that should do the trick

{!! app()->chartbar->render("BarChart", $userInfo) !!}

EDIT: i assumed $user_info was already in the correct format but you couldn’t figure out how to display it. Reading your comments i better understand your needs.
I don’t know which data format chartJS and what you want to display. Looking $data example and your DB query, i assume you want to display data in the format “role_id” => count(*)
so, in that case, i would do something like

$userArray = array();
foreach($user_info as $row) {
    $userArray[$row->role_id] = $row->total;
}
return view('admin.main')->with('userInfo',$userArray);

0đź‘Ť

Looks like you need to put the data in the right format:

public function getMainAdminBackend() {
    $user_info = \DB::table('users')
                    ->select('role_id', \DB::raw('count(*) as total'))
                    ->groupBy('role_id')
                    ->get();

    $data = [];

    if ($user_info) {
        foreach ($user_info as $month => $value) {
            $data[date("M", mktime(0, 0, 0, $month, 1, 0))] = [$value];
        }
    }

    return view('admin.main')->with('userInfo', $data);
}

Leave a comment