【发布时间】:2021-10-14 05:13:38
【问题描述】:
我在dd($request->all())中有以下数组
"question_id" => array:2 [▼
0 => "1"
1 => "2"
]
"answers" => array:2 [▼
0 => "1 year"
1 => "yes"
]
我需要循环这个数组以存储在this table
这个请求来自我创建的动态表单。 这个方法我试过了
$data = $request->all();
$questions = $data['question_id'];
$answers = $data['answers'];
$dataanswer = [];
foreach($answers as $answer){
$dataanswer[] = [
'question_id' => $questions,
'answer' => $answers
];
}
Answers::insert($dataanswer);
但上面写着Array to string conversion,请帮帮我。
【问题讨论】:
-
因为
$questions和$answers中有数组,所以'question_id' => $questions,会尝试将数组插入question_id 列 -
@porloscerrosΨ 感谢您的回答,我如何从请求的数组中获取值?所以我可以放入表中
-
这取决于DB中表的结构。 @sta 的答案很好,您不需要循环,我认为这就是我要回答的问题的提出方式。如果该答案出于任何原因对您没有帮助,请通过添加更多信息来编辑问题
标签: laravel loops foreach store