vendredi 13 avril 2018

Pass data from controller to view into script - LARAVEL

I need to pass a variable from the controller to the view, to use it in the script and configure the Highstock graph. I have a problem with the date conversion, and the use of arrays. Unfortunately, the data are not included in the chart. I receive the data correctly in the view, but I think we need to "format" it via json_encode or whatever. Can you tell me why and how can I solve the problem?

statistiche.blade.php

@section('content')

<div id="container" style="height: 400px; min-width: 310px"></div>
@stop
@section('css')@stop@section('js')
<script>

        var data = [@php echo $data @endphp];


        // Create the chart
        Highcharts.stockChart('container', {

            rangeSelector: {
                selected: 1
            },

            title: {
                text: 'Richieste ricevute'
            },

            series: [{
                name: 'Richieste ricevute',
                data: data,
                tooltip: {
                    valueDecimals: 2
                }
            }]
        });

</script>
@stop

statisticheController.php

    public function index(){
    /* calcolo il totale delle richieste ricevute */
    $richieste = Richiesta::groupBy(DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d")'))
        ->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d") as data'), DB::raw('count(*) as richieste_totali'))
        ->get();


    foreach($richieste as $richiesta) {

        $data[] = [$richiesta->data, $richiesta->richieste_totali];
    }

    return view('layouts.statistiche', compact( 'data'));
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire