我想为我的数据库播种,但所有内容都插入一个字段(JOBURL
)
EmploisTableSeeder.php
use Illuminate\Database\Seeder;
use App\Emploi;
class EmploisTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*
*php artisan db:seed --class=EmploisTableSeeder
*/
public function run()
{
//
$json = file_get_contents('http://www.ottawacityjobs.ca/en/data/');
$emplois = json_decode($json);
$emplois = $emplois->jobs;
//$emp = Emploi::create($array['order']);
//$emp = Emploi::create($emplois);
for ($i = 0; $i < count($emplois); $i++) {
//print_r($emplois[$i]->JOBREF);
//print_r("\n");
Emploi::insert(
array(
array('JOBURL' => isset($emplois[$i]->JOBURL) ? $emplois[$i]->JOBURL : ' '),
array('SALARYMAX' => isset($emplois[$i]->SALARYMAX)? $emplois[$i]->SALARYMAX : ' ' ),
array('SALARYMIN' => isset($emplois[$i]->SALARYMIN)? $emplois[$i]->SALARYMIN : ' ' ),
array('SALARYTYPE' => isset($emplois[$i]->SALARYTYPE)? $emplois[$i]->SALARYTYPE : ' ' ),
array('NAME' => isset($emplois[$i]->NAME)? $emplois[$i]->NAME : ' ' ),
array('POSITION' => isset($emplois[$i]->POSITION)? $emplois[$i]->POSITION : ' ' ),
array('JOBREF' => isset($emplois[$i]->JOBREF) ? $emplois[$i]->JOBREF : ' '),
array('JOB_SUMMARY' => isset($emplois[$i]->JOB_SUMMARY) ? $emplois[$i]->JOB_SUMMARY : ' '),
array('POSTDATE' => isset($emplois[$i]->POSTDATE) ? $emplois[$i]->POSTDATE : ' '),
array('EXPIRYDATE' => isset($emplois[$i]->EXPIRYDATE)? $emplois[$i]->EXPIRYDATE : ' ' ),
array('KNOWLEDGE' => isset($emplois[$i]->KNOWLEDGE) ? $emplois[$i]->KNOWLEDGE : ' '),
array('LANGUAGE_CERTIFICATES' => isset($emplois[$i]->LANGUAGE_CERTIFICATES) ? $emplois[$i]->LANGUAGE_CERTIFICATES : ' ' ),
array('EDUCATIONANDEXP' => isset($emplois[$i]->EDUCATIONANDEXP) ? $emplois[$i]->EDUCATIONANDEXP : ' ' ),
array('COMPANY_DESC' => isset( $emplois[$i]->COMPANY_DESC) ? $emplois[$i]->COMPANY_DESC : ' '),
)//end array
);//end Emploi::insert
}
//print_r(dd($emplois));
}//end run function
}// end class EmploiTableSeeder
总的来说,我有大约90个json对象可以循环,但所有内容都插入到一个字段中。
答案 0 :(得分:1)
在单个数组中包含每个字段。这样做。
array(
'JOBURL' => isset($emplois[$i]->JOBURL) ? $emplois[$i]->JOBURL : ' ',
'SALARYMAX' => isset($emplois[$i]->SALARYMAX)? $emplois[$i]->SALARYMAX : ' ',
),