当我从API获得信息并想要在我的数据库中填充它时,我得到一个错误:
数组到字符串的转换
注意:我的表是json类型的数组从数据库。
控制器:
public function fetch()
{
$response = Http::get('some api');
$countries = json_decode($response, true);
foreach ($countries as $country)
{
$api = new Country();
$api['code'] = $country['code'];
$api['name'] = ['en' => $country['name']['en'], 'ka' => $country['name']['ka']];
$api->save();
}
return 'DONE';
}
我也尝试过其他方式,如json_decode($reponse->body())
,然后$api->name = $country->name->en
等…
我相信这个保存处理方法应该可以工作:
foreach ($countries as $country)
{
$api = new Country();
$api->code = $country['code'];
$api->name = json_encode(['en' => $country['name']['en'], 'ka' => $country['name']['ka']]);
$api->save();
}