4

I want to display data from the controller and throw it in the view as a dataset, but I have trouble how to write a loop on scriptjava.

This is the code that I have tried, and still fails

This my controller :

laravel
    $query =  DB::table('app_history_calls')
              ->SELECT(DB::raw("(MONTH(date)) as bulan"))
              ->GROUPBY('date',DB::raw("MONTH(date)"))
              ->ORDERBY('date','asc')->get();


            return view('dashboard.index',compact('query'));
        }

and this my view:

laravel
 <script type="text/javascript">

var month= [
<?php foreach ($query as $bulan): ?>

 <?php echo (string)$bulan ?>

<?php endforeach; ?>
];
</script>

I expected the result will be like this:

"var month= [1,2,3,4,5,6,7,8,9,10,11,12];"

So that it can be read on the dataset

delfin
  • 41
  • 3

1 Answers1

1
  $data['query'] =  DB::table('app_history_calls')
              ->SELECT(DB::raw("(MONTH(date)) as bulan"))
              ->GROUPBY('date',DB::raw("MONTH(date)"))
              ->ORDERBY('date','asc')->get();


            return view('dashboard.index')->withdata($data);
        }
 <script type="text/javascript">

var month= [
<?php foreach ($data['query'] as $bulan): ?>

 <?php echo (string)$bulan ?>

<?php endforeach; ?>
];
</script>

Replace above code with your code i passed value in $data array and in for loop read $data['query'] in js

Sujal Patel
  • 2,444
  • 1
  • 19
  • 38