我正在努力访问Laravel中的所有输入数组数据,因为我之前没有遇到任何问题:例如,$ _POST ['name'] [$ row]
我也收到以下错误,但我认为这是因为NULL数据 SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'0'
POST数据:
name Array ( [0] => name1 [1] => name2 )
dob Array ( [0] => 06/23/16 [1] => 06/03/16 )
gender Array ( [0] => 1 [1] => 0 )
循环:
$jobID = 2;
$data = array();
foreach(Input::get('name') as $row=>$name){
$dob = Input::get('dob'.$row);
$gender = Input::get('gender'.$row);
$data[] = "['job_id' => '$jobID', 'first_name' => '$name', 'dob' => '$dob', 'gender' => '$gender']";
}
$data_insert = implode(',', $data);
if(!empty($name)) {
DB::table('job_data')->insert([
$data_insert
]);
}
结果SQL:
insert into `job_data` (`0`)
values (
['job_id' => '2', 'first_name' => 'name1', 'dob' => '', 'gender' => ''],
['job_id' => '2', 'first_name' => 'name2', 'dob' => '', 'gender' => '']
)
答案 0 :(得分:1)
您不必将数据字符串化,只需使用此
即可$data[] = [
'job_id' => $jobID,
'first_name' => $name,
'dob' => $dob,
'gender' => $gender
];
并插入
DB::table('job_data')->insert($data);