Hello, I need help with a foreach inside code js, I try to list from my database but I can not make it work.
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Pregunta', 'Veces respondida'],
@foreach ($pastel as $pastels)
["{{$pastels->titulo}}","{{$pastels->respuesta}}"],
@endforeach]);
var options = {
title: '{{$pastels->titulo}}'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
Here is the driver code passing the sql statement with a variable.
public function ver_graficos() {
$pastel = DB::table('pregunta')
->join('respuesta','pregunta.id_pregunta', '=' ,'respuesta.id_respuesta')
->select('pregunta.titulo', 'respuesta.respuesta')
->get();
return view('respuesta.ver1',['pastel'=>$pastel]);
}
The error is that it does not enter the cycle and does not show me anything in the graph. HELP ME!