我想以多种形式记录设备,但总是会出现错误。 未定义的索引:名称
public function EquipCheck(Request $request) {
$user = User::where('username', $request -> username) -> firstOrFail();
if ($request -> token == $user -> token) {
$item_detail_id = CheckLog::where('user_id', $user -> id) -> orderBy('id', 'desc') -> first() -> item_detail_id;
$new_form = new Form;
$new_form -> item_detail_id = $item_detail_id;
$new_form -> user_id = User::where('username', $request -> username) -> firstOrFail() -> id;
$new_form -> created_time = Carbon::now() -> format('Y-m-d H:i:s');
$new_form -> save();
$form = Form::with('eqpt') -> where('item_detail_id', $item_detail_id) -> orderBy('created_time', 'desc') -> first();
$data = $request -> eqpt;
$i = 0;
$sql = array();
while($i < count($cyldata['serie'])){
$sql[] = array(
'form_id' => $form -> id,
'name' => $data['name'][$i],
'unit' => $data['unit'][$i],
'quantity' => $data['quantity'][$i],
'check_quantity' => $data['check_quantity'][$i],
);
$i++;
}
DB::table('eqpt') -> insert($sql);
return "Success";
}
}
当我使用以下代码时,数据库可以插入数组。
$dataSet[] = [
'form_id' => $form -> id,
'name' => 'test',
'unit' => 'box',
'quantity' => 20,
'check_quantity' => 20,
];
DB::table('eqpt') -> insert($dataSet);
答案 0 :(得分:2)
$ data 包含一系列对象。 $ data ['name'] 是指该数组的属性,而您应该访问该数组中对象的属性。
替换
$sql[] = array(
'form_id' => $form -> id,
'name' => $data['name'][$i],
'unit' => $data['unit'][$i],
'quantity' => $data['quantity'][$i],
'check_quantity' => $data['check_quantity'][$i],
);
与
$sql[] = array(
'form_id' => $form -> id,
'name' => $data[$i]['name'],
'unit' => $data[$i]['unit'],
'quantity' => $data[$i]['quantity'],
'check_quantity' => $data[$i]['check_quantity'],
);