如何使用 Laravel 中每行具有 MultiSelect 下拉列表的动态 Table 保存表单

How to Save Form with the Dynamic Table having MultiSelect Dropdown in each row in Laravel

Multiselect Drop Down of Color and Size in Dynamic Table with name attribute

在我的表单中,我有动态 Table,每行有多个 Select 同名 color_id[] 和 size_id[] 的下拉菜单。我不知道如何将多个选定值保存为数据库中每一行中的逗号分隔值。在这里,我试图保存在数据库中,但没有用。 HTML代码:

   <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>

Laravel 在控制器中保存代码:

 $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);

我在每一行的颜色和大小相同的列中取了两个名为 color_arrays[] 和 size_arrays[] 的隐藏输入框。我在下面写了 java 脚本函数,以从 multi-select drop-down 的颜色和大小中获取逗号分隔值。我将隐藏的输入框值保存到数据库。

$(document).on('change', 'select[name^="color_id[]"],select[name^="size_id[]"]', function(){CalculateQtyRowPros2($(this).closest("tr"));});

function CalculateQtyRowPros2(row){   
var color_id=row.find('select[name^="color_id[]"]').val().join(",");
var size_id=row.find('select[name^="size_id[]"]').val().join(",");
row.find('input[name^="color_arrays[]"]').val(color_id);
row.find('input[name^="size_arrays[]"]').val(size_id);}

对我有用。