简体   繁体   中英

passing an array from controller to js in laravel

I'm making a morris line in my index page and I'm trying to pass data to it from my controller controller:

$chartData=  DB::table('transactions')
        ->select('v_type',DB::raw("MONTH(transaction_created_at)"),DB::raw('sum(v_value)'))
        ->groupby('v_type',DB::raw("MONTH(transaction_created_at)"))
        ->get();

  return view('layouts/dashboard/index', ['tickets' => $tickets , 'types'=> $types,'response'=>json_encode($all),'chartData' => $chartData]);

index.blade.php

 .......<div class="row clearfix">

                <div class="card">

                    <div class="header">
                        <h4>Charts</h4>
                    </div>
                        <div class="body">
                            <div id="pushups" class="graph-wrapper" > </div>

                        </div>
                    </div>
                </div>
            </div>
  ......
 @section('extra-script')
    {{Html::script('plugins/jquery-countto/jquery.countTo.js')}}
    {{Html::script('plugins/raphael/raphael.min.js')}}
    {{Html::script('plugins/morrisjs/morris.js')}}
    {{Html::script('plugins/chartjs/Chart.bundle.js')}}
    {{Html::script('plugins/flot-charts/jquery.flot.js')}}
    {{Html::script('plugins/flot-charts/jquery.flot.resize.js')}}
    {{Html::script('plugins/flot-charts/jquery.flot.pie.js')}}
    {{Html::script('plugins/flot-charts/jquery.flot.categories.js')}}
    {{Html::script('plugins/flot-charts/jquery.flot.time.js')}}
    {{Html::script('plugins/jquery-sparkline/jquery.sparkline.js')}}

    {{Html::script('js/pages/index.js')}}

    @endsection

and in my index.js i have this function

function initLineChart() {
Morris.Line({
    element: 'pushups',
    data: [
        { day: 'Monday', pushups: 20, beers: 2 },
        { day: 'Tuesday', pushups: 10, beers: 2 },
        { day: 'Wednesday', pushups: 5, beers: 3 },
        { day: 'Thursday', pushups: 5, beers: 4 },
        { day: 'Friday', pushups: 20, beers: 1 }
    ],
    xkey: 'day',
    parseTime: false,
    ykeys: ['pushups','beers'],
    labels: ['Pushups','Beers'],
    lineColors: ['#373651','#E65A26']
});
}

here I'm using this data just for testing however i would like to use the $chartdata to fill this morris line

the chartdata json looks like this

[
  {
    "v_type": "money_value",
    "MONTH(transaction_created_at)": 9,
    "sum(v_value)": 15.5
  },
  {
    "v_type": "tariff_switch",
    "MONTH(transaction_created_at)": 9,
    "sum(v_value)": 3
  },
  .......]

so how can i do that

Thank you in advance.

You can pass a php array to index.js file like this:

index.blade.php

<script type="text/javascript">var chart_data =<?php echo chartData; ?>;</script>
{{Html::script('js/pages/index.js')}}

variable chart_data will be accessible in index.js file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM