I am not able to perform this insertion to an array, considering that I have this example static array in php. Where I intend to display the record in a bar graph with the canvasjs library, where one of its attributes is to pass an array like the following:
$dataPoints = array(
array("label"=> "Matematicas", "y"=>22),
array("label"=> "Ingles", "y"=> 34),
array("label"=> "Fisica", "y"=> 4),
array("label"=> "Quimica", "y"=> 13),
array("label"=> "Arte", "y"=> 50)
);
I do this part in the controller (by the way I am working with Laravel) but now I want to get the data from a database. This is my method of my controller
public function index()
{
$dataPoints= DB::table('materias')->get();
return View('reporteMaterias',compact('dataPoints'));
}
Now, if I pass the static array and load it to canvarjs in one of its parameters, it works correctly like this:
data: [{
type: "column",
showInLegend: true,
legendMarkerColor: "grey",
legendText: "Materias ",
dataPoints: @json($dataPoints)
}]
});
So, going back to the main point, how would you make a dynamic array but making a query to the database to make it dynamic? yes considering that this is my query $dataPoints= DB::table('materias')->get();
Now how do I pass it as the static array mentioned above?
You should indicate the fields of your table
materias
, but assuming that there is a fieldnombre
and another calledcampo_y
, it would be: