语法错误,laravel中出现意外的“ foreach”(T_FOREACH)

时间:2019-06-17 07:04:11

标签: laravel eloquent laravel-5.8

我正在尝试将数据从db获取到条形图中,但是它给出了以上错误,

控制器代码:

   public function chart($id)
   {
    $data = File::findOrFail($id);
    $result = \DB::table('importpdfs')
                ->where('Battery','=','Verbal')
                ->orderBy('SAS', 'ASC')
                ->get();
    @foreach ($pdfdata => $data)
      {{ $data->SAS }}
    @endforeach
    return response()->json($result);
  }

bladefile:

     <div class="container">
          <canvas id="myChart"></canvas>
        </div>
        @push('scripts')
        <script 
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
      @endpush
      <script>
        var url = "{{url('importpdfs')}}";
          var Bat = new Array();
          var Sas = new Array();
            //$(document).ready(function(){
              //$.get(url, function(response){
                //response.forEach(function(data){
                  //Bat.push(data.Battery);
                  //Sas.push(data.SAS);
            //});
        let myChart = document.getElementById('myChart').getContext('2d');

          // Global Options
          Chart.defaults.global.defaultFontFamily = 'Lato';
          Chart.defaults.global.defaultFontSize = 18;
          Chart.defaults.global.defaultFontColor = '#777';

          let massPopChart = new Chart(myChart, {
            type:'bar', // bar, horizontalBar, pie, line, doughnut, radar, 
          polarArea
        data:{
          labels:['Verbal', 'Quantitative', 'Non-Verbal', 'Spatial'],
            datasets:[{
              label:'SAS',
                data:[
                  120,
                  112,
                  121,
                  111,
                  110,
                ],
          //backgroundColor:'green',
          backgroundColor:[
            'rgba(54, 162, 235, 0.6)',
            'rgba(54, 162, 235, 0.6)',
            'rgba(54, 162, 235, 0.6)',
            'rgba(54, 162, 235, 0.6)',
            'rgba(54, 162, 235, 0.6)',
            'rgba(54, 162, 235, 0.6)',
            'rgba(54, 162, 235, 0.6)',
          ],
          borderWidth:1,
          borderColor:'#777',
          hoverBorderWidth:1,
          hoverBorderColor:'#000'
        }]
      },
      options:{
        title:{
          display:true,
          text:'Bar Chart',
          fontSize:25
        },
        legend:{
          display:true,
          position:'right',
          labels:{
            fontColor:'#000'
          }
        },
        layout:{
          padding:{
            left:10,
            right:0,
            bottom:0,
            top:0
                }
              },
              tooltips:{
                enabled:true
              }
            }
          });
        </script>

如何解决?

1 个答案:

答案 0 :(得分:0)

为什么要尝试将刀片PHP放入控制器中?严格来说,这应该是标准的PHP。

让前端处理数据(尽管实施起来比较麻烦,但这意味着服务器在处理操作时会减少负载)。我建议您删除控制器中的foreach,然后将failed返回前端,并在那里处理foreach。