在Laravel中实现动态表单保存时如何处理每行多选下拉表格
<p>在我的表单中,我有一个动态表格,每一行中都有相同名称的多选下拉菜单color_id[]和size_id[]。我不知道如何将多个选择的值保存为每一行中的逗号分隔的值存储在数据库中。我在这里尝试保存到数据库,但是没有起作用。</p>
<p>HTML代码:</p>
<pre class="brush:php;toolbar:false;"><table><tbody><td><select name="color_id[]" class="select2" id="color_id" style="width:200px; height:100px;" required multiple></select></td><td> <select name="size_id[]" class="select2" id="size_id" style="width:200px; height:100px;" required multiple> </select></td></tbody></table></pre>
<p>在控制器中保存的Laravel代码:</p>
<pre class="brush:php;toolbar:false;">$class_ids = $request->input('class_ids');
for($x=0; $x<count($class_ids); $x++) {
# code...
$color_ids = implode(',', $request->color_id[$x]);
$size_ids = implode(',', $request->size_id[$x]);
$data3[]=array(
'bom_code'=>$TrNo,
'bom_date'=>$request->bom_date,
'cost_type_id'=>$request->cost_type_id,
'Ac_code'=>$request->Ac_code,
'season_id'=>$request->season_id,
'currency_id'=>$request->currency_id,
'item_code' => $request->item_codes[$x],
'class_id' => $request->class_ids[$x],
'description' => $request->descriptions[$x],
'color_id' => $color_ids,
'size_array' => $size_ids,
'consumption' => $request->consumptions[$x],
'unit_id'=> $request->unit_ids[$x],
'rate_per_unit' => $request->rate_per_units[$x],
'wastage' => $request->wastages[$x],
'bom_qty' => $request->bom_qtys[$x],
'total_amount' => $request->total_amounts[$x],
);
}
BOMSewingTrimsDetailModel::insert($data3);</pre>