0

<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?

1 Answer 1

1

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) !!}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!!
You are welcome sir, if this answer solved the issue you can mark it as the correct answer to help future readers! Have a good code day :-)

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.