Laravel Charts: Passing variable to handler function

吃可爱长大的小学妹 提交于 2021-01-04 06:35:52

问题


I followed the guide on https://charts.erik.cat/guide/ to install Chartisan with echarts in my Laravel 7.4 framework. Everything works fine and I receive a chart.

The chart is triggered with this handler function:

public function handler(Request $request): Chartisan
    {
        return Chartisan::build()
            ->labels(['First', 'Second', 'Third'])
            ->dataset('Sample', [1, 2, 3])
            ->dataset('Sample 2', [3, 2, 1]);
    }

The dataset shall be pulled from my database therefore I modify the code to:

public function handler(Request $request): Chartisan
    {
        $data = DB::Users->where('id',$id)->find(1); //$id is not defined

        return Chartisan::build()
            ->labels(['First', 'Second', 'Third'])
            ->dataset('Sample', [1, 2, 3])
            ->dataset('Sample 2', [3, 2, 1]);
    }

Of course this doesn't work because $id is not defined. How and where can I pass data to the handler function? How can I use the $request variable if it is not a form?

The chart is called via the api/chart/SampleChart route. Where is this route defined? Could I pass data to the route? It is not included in routes/api.php or web.php.

All Laravel/Chart examples with the handler function are using ALL data from the database table without restrictions. I did not find an example which passes data.

global $id does not work to get access to $id.


回答1:


You can easily pass parameters from your view to your chart url with "?":

http://localhost/public/api/chart/sample_chart?id=1

like this:

const chart = new Chartisan({
        el: '#chart',
        url: "@chart('sample_chart')" + "?id=1"
});

this part "?id=1" I would do something like this "?id={{ $id_came_from_controller }}"

and then in your handle(Request $request) method you can access your $id like this:

$id = $request->id;
$data = DB::Users->where('id', $id)->first();

P.S.

if you need several parameters, you can pass like this:

http://localhost/public/api/chart/sample_chart?first_param=123&second_param=456&third_param=789

and retrieve:

$request->first_param;
$request->second_param;
$request->third_param;



回答2:


The handler method is used to show static example in the docs.

You can directly instantiate the SampleChart, by adding it into the controller, If you have SampleChart inside App->Charts->SampleChart.php, you can add use App\Charts\SampleChart

Add a contructor to your sample chart, not know if they provide in recent versions by default,

class SampleChart extends Basechart
    public function __construct(){
        parent::__contruct();
    }

Then create instance

$samplechart = new SampleChart;
$data = DB::Users->find(1); // either use DB::Users->where('id',$id)->first();
/**
* let $data looks like, $data->name = "bhucho";$data->id = 2;$data->hobby = stoverflow; 
*/

$samplechart->labels = (array_keys($data));
$samplechart->dataset = (array_values($data));

return view('yourview', compact('samplechart'));

Also add these to your SampleChart as mentioned in chart_configuration.

 public ?string $name = 'custom_chart_name';
 public ?string $routeName = 'chart_route_name';


来源:https://stackoverflow.com/questions/64873944/laravel-charts-passing-variable-to-handler-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!