我正在尝试播种数据库表。我收到了以下错误。
public function run()
{
//
$json = file_get_contents('http://www.ottawacityjobs.ca/en/data/');
$emplois = json_decode($json);
foreach ($emplois as $item) {
Emploi::insert(
array(
array('JOBURL' => $item->JOBURL ),
array('SALARYMAX' => $item->SALARYMAX ),
array('SALARYMIN' => $item->SALARYMIN ),
array('SALARYTYPE' => $item->SALARYTYPE ),
array('NAME' => $item->NAME ),
array('POSITION' => $item->POSITION ),
array('JOBREF' => $item->JOBREF ),
array('JOB_SUMMARY' => $item->JOB_SUMMARY ),
array('POSTDATE' => $item->POSTDATE ),
array('EXPIRYDATE' => $item->EXPIRYDATE ),
array('KNOWLEDGE' => $item->KNOWLEDGE ),
array('LANGUAGE_CERTIFICATES' => $item->LANGUAGE_CERTIFICATES ),
array('EDUCATIONANDEXP' => $item->EDUCATIONANDEXP ),
array('COMPANY_DESC' => $item->COMPANY_DESC ),
));
}//end foreach
答案 0 :(得分:2)
您提供的网址(http://www.ottawacityjobs.ca/en/data/
)在您访问时不会显示数据。 json的结构如下:
{
jobs: [
{...},
{...},
{...}
]
}
因此,您的代码需要遍历jobs
属性:
public function run()
{
//
$json = file_get_contents('http://www.ottawacityjobs.ca/en/data/');
$emplois = json_decode($json);
foreach ($emplois->jobs as $item) {
Emploi::insert(
array(
array('JOBURL' => $item->JOBURL ),
array('SALARYMAX' => $item->SALARYMAX ),
array('SALARYMIN' => $item->SALARYMIN ),
array('SALARYTYPE' => $item->SALARYTYPE ),
array('NAME' => $item->NAME ),
array('POSITION' => $item->POSITION ),
array('JOBREF' => $item->JOBREF ),
array('JOB_SUMMARY' => $item->JOB_SUMMARY ),
array('POSTDATE' => $item->POSTDATE ),
array('EXPIRYDATE' => $item->EXPIRYDATE ),
array('KNOWLEDGE' => $item->KNOWLEDGE ),
array('LANGUAGE_CERTIFICATES' => $item->LANGUAGE_CERTIFICATES ),
array('EDUCATIONANDEXP' => $item->EDUCATIONANDEXP ),
array('COMPANY_DESC' => $item->COMPANY_DESC ),
));
}//end foreach