0

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'));
}

1 Answer 1

3

Instead of

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

you can just have

var data = @json($data);

Also, instead of running @php echo $stuff; @endphp you can also echo stuff like {{$stuff}}

Check this docs https://laravel.com/docs/5.6/blade

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.