anonymous
anonymous

Reputation: 257

Unknown error laravel using javascript function

<script type="text/javascript">

      var data = {
      labels: ['{{ $labels[0] }}', '{{ $labels[1] }}', '{{ $labels[2] }}', '{{ $labels[3] }}'],

      datasets: [
        {
          fillColor: "rgba(220,220,220,0.2)",
          strokeColor: "rgba(220,220,220,1)",
          pointColor: "rgba(220,220,220,1)",
          pointStrokeColor: "#fff",
          pointHighlightFill: "#fff",
          pointHighlightStroke: "rgba(220,220,220,1)",
          data: ['{{ $data[0] }}', '{{ $data[0] }}', '{{ $data[0] }}', '{{ $data[0] }}']
        },
      ]
    };

    var context = document.querySelector('#graph').getContext('2d');

    new Chart(context).Line(data);

</script>
public function tracker()
  {
    $statistics = DiraStatistics::all();
    foreach ($statistics as $key => $statistic) {
        labels[] =  $statistic->date_access;
        data[] =  $statistic->question_asked;
    }
     return view('AltHr.Chatbot.tracker', compact('labels','data'));
  }

Hi guys so im doing a simple graph in laravel using a javascript function in my controller i have written to get the 2 data i need for the graph but im getting this error:

FatalErrorException in TrackerController.php line 27: Cannot use temporary expression in write context

How do i solve this?

Upvotes: 0

Views: 111

Answers (1)

aaron0207
aaron0207

Reputation: 2333

Your arrays assignments are bad, you have to initialize them before and then push items inside.

public function tracker()
  {
    $statistics = DiraStatistics::all();
    $labels = [];
    $data = [];
    foreach ($statistics as $key => $statistic) {
        array_push($labels,$statistic->date_access);
        array_push($data,$statistic->question_asked);
    }
     return view('AltHr.Chatbot.tracker', compact('labels','data'));
  }

And you can use your arrays in js like this (at blade files):

{!! json_encode($labels) !!}

Upvotes: 1

Related Questions