如何在laravel中为google条形图创建查询?
这是我想画的条形图,我在查询中被打动了 我的数据库看起来像是我想显示2020年国家人口的地方如何在laravel中为google条形图创建查询?,laravel,Laravel,这是我想画的条形图,我在查询中被打动了 我的数据库看起来像是我想显示2020年国家人口的地方 id | country_name | population | created_at 1 | USA | 2000000000 | 2020-02-04 2 | China | 2200000000 | 2020-02-04 3 | Russia | 12000000 | 2020-04-02 我的问题是这样的。我只是试图建立查询,但没
id | country_name | population | created_at
1 | USA | 2000000000 | 2020-02-04
2 | China | 2200000000 | 2020-02-04
3 | Russia | 12000000 | 2020-04-02
我的问题是这样的。我只是试图建立查询,但没有得到结果。可能有一些错误。首先我使$countries变量得到所有国家
public function barChart(){
$countries=Country::get();
$graphic_header=['Year'];
$countrydata=[];
$actionDate=[];
$country_name=[];
foreach ($countries as $country) {
array_push($country_name, $country->name);
array_push($actionDate, date('Y', strtotime($country->created_at)));
$actionDate = array_unique($actionDate);
}
$graphic_header=array_merge($graphic_header,$country_name); //Dynamic Header
array_push($countrydata,$graphic_header);
foreach($actionDate as $d) {
$d = [$d];
$d = array_pad($d, sizeof($graphic_header), 0);
array_push($data, $d);
}
foreach ($countries as $country){
$date = date('Y', strtotime($country->created_at));
foreach ($data as $in =>$gd){
if ($date == $gd[0]) {
$index = (array_search($country->country_name, $graphic_header));
$country_data[$in][$index] = $country->population;
}
}
}
return response()->json([
'data'=> $country_data,
]);
}
只需使用这个groupBy收集方法
$countries=Country::get();
$countries = $countries->groupBy('country_name');
$countries->toArray();
只需使用这个groupBy收集方法
$countries=Country::get();
$countries = $countries->groupBy('country_name');
$countries->toArray();