Laravel:从url播种数据库时出错

时间:2017-06-05 19:03:44

标签: laravel laravel-5 laravel-seeding

我正在尝试播种数据库表。我收到了以下错误。

enter image description here

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

1 个答案:

答案 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