3 回答

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊
我收到 ErrorException (E_NOTICE) 未定義索引:NewConfirmed
當(dāng)我做了這樣的事情時(shí)
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貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
在 foreach 內(nèi)部,您應(yīng)該引用 $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']
]);
另一點(diǎn)是國(guó)家也是一個(gè)數(shù)組,因此您可能應(yīng)該為國(guó)家創(chuàng)建另一個(gè) foreach。
foreach ($data['Countries'] as $country) { }

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊
您必須為Global::create()and的返回值分配一個(gè)變量Country::create()。像這樣$global = Global::create(...)運(yùn)行$global->save()將其存儲(chǔ)在數(shù)據(jù)庫(kù)中。
編輯:根據(jù)您最新的回復(fù),您不應(yīng)該迭代數(shù)據(jù),因?yàn)榉祷氐臄?shù)據(jù)不是數(shù)組。對(duì)于全局變量,您不需要迭代。你可以這樣做:
$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 關(guān)注
- 205 瀏覽
添加回答
舉報(bào)