首页 > 解决方案 > Undefined variable: sales_chart (Laravel Chart)

问题描述

I'm a beginner in php and in Laravel. I wanted to add charts to my dashboard. The charts can be displayed on a new separate page but when I include it to my dashboard, it gives me an

error Undefined variable: sales_chart (View: C:\xampp\htdocs\Laravel\microelephant_updated~\microelephant\resources\views\chart.blade.php)

This is what's in my chart.blade.php


<div class='flex'>
    <div class="w-1/2">
     {!! $sales_chart-> container() !!} 
    </div>

{!! $sales_chart-> script() !!}


</div>

My ChartController


namespace App\Http\Controllers;

use App\Invoice;

use Illuminate\Http\Request;
use App\Charts\SalesChart;


class ChartController extends Controller
{
    public function index(Request $request)
    {
       $sales = Invoice::orderBy('created_at')->pluck('total','created_at');
       $sales_chart = new SalesChart;

       $sales_chart->labels($sales->keys());
       $sales_chart->dataset('Sales', 'line', $sales->values());

       return view('chart', compact('sales_chart'));
    }
}

How I include the chart in my dashboard

<div class="main-content">
    @include('chart')
</div> 

This is my Route Route::get('/chart', 'ChartController@index');

There is something wrong with my code but I don't know where it is. Please help :(

标签: phplaravellaravel-charts

解决方案


推荐阅读