3 回答
TA贡献1887条经验 获得超5个赞
我收到 ErrorException (E_NOTICE) 未定义索引:NewConfirmed
当我做了这样的事情时
foreach($data['Countries'] as $country){
$counter= Country::create([
'Country'=> $country['Country'],
'CountryCode'=>$country['CountryCode'],
'Slug'=>$country['Slug'],
'Date'=>$country['Date'],
'NewConfirmed'=>$country['NewConfirmed'],
'TotalConfirmed'=>$country['TotalConfirmed'],
'NewRecovered'=>$country['NewRecovered'],
'TotalRecovered'=>$country['TotalRecovered'],
'NewDeaths'=>$country['NewDeaths'],
'TotalDeaths'=>$country['TotalDeaths']
]);
}
foreach($data as $da){
$globals=Globas::create([
'NewConfirmed'=>$da['NewConfirmed'],
'TotalConfirmed'=>$da['TotalConfirmed'],
'NewRecovered'=>$da['NewRecovered'],
'TotalRecovered'=>$da['TotalRecovered'],
'NewDeaths'=>$da['NewDeaths'],
'TotalDeaths'=>$da['TotalDeaths']
]);
}
}
TA贡献1772条经验 获得超5个赞
在 foreach 内部,您应该引用 $da 变量,而不是该位置的 $data:
Country::create([
'Country'=> $data['Country'],
'CountryCode'=>$data['CountryCode'],
'Slug'=>$data['Slug'],
'Date'=>$data['Date'],
'NewConfirmed'=>$data['NewConfirmed'],
'TotalConfirmed'=>$data['TotalConfirmed'],
'NewRecovered'=>$data['NewRecovered'],
'TotalRecovered'=>$data['TotalRecovered'],
'NewDeaths'=>$data['NewDeaths'],
'TotalDeaths'=>$data['TotalDeaths']
]);
另一点是国家也是一个数组,因此您可能应该为国家创建另一个 foreach。
foreach ($data['Countries'] as $country) { }
TA贡献1852条经验 获得超7个赞
您必须为Global::create()and的返回值分配一个变量Country::create()。像这样$global = Global::create(...)运行$global->save()将其存储在数据库中。
编辑:根据您最新的回复,您不应该迭代数据,因为返回的数据不是数组。对于全局变量,您不需要迭代。你可以这样做:
$response = $client->request('GET', 'https://api.covid19api.com/summary');
$data = json_decode($response->getBody()->getContents(),true);
$globals=Globas::create([
'NewConfirmed'=>$data['NewConfirmed'],
'TotalConfirmed'=>$data['TotalConfirmed'],
'NewRecovered'=>$data['NewRecovered'],
'TotalRecovered'=>$data['TotalRecovered'],
'NewDeaths'=>$data['NewDeaths'],
'TotalDeaths'=>$data['TotalDeaths']
]);
$globals->save();
foreach($data['Countries'] as $country) {
$country = Country::create([
'Country'=> $country['Country'],
'CountryCode'=>$country['CountryCode'],
'Slug'=>$country['Slug'],
'Date'=>$country['Date'],
'NewConfirmed'=>$country['NewConfirmed'],
'TotalConfirmed'=>$country['TotalConfirmed'],
'NewRecovered'=>$country['NewRecovered'],
'TotalRecovered'=>$country['TotalRecovered'],
'NewDeaths'=>$country['NewDeaths'],
'TotalDeaths'=>$country['TotalDeaths']
]);
$country->save();
}
- 3 回答
- 0 关注
- 145 浏览
添加回答
举报