【发布时间】:2019-01-04 18:21:17
【问题描述】:
我有一个动态可排序表,我可以在其中添加或删除行,例如 enter image description here
现在我正在努力将这些数据插入我的数据库 (Mysql)。
这是我的查看页面
<table class="table table-hover table-sortable" id="tab_logic" name="DataTable">
<thead>
<tr class="text-center">
<td style="width: 40%;">Process Name</td>
<td style="width: 30%;">Machine Name</td>
<td style="width: 10%;">Machine Qty</td>
<td style="width: 10%;">SMV</td>
<td style="width: 10%;">Action</td>
</tr>
</thead>
<tbody>
<tr id='addr0' data-id="0" class="hidden">
<td data-name="ProcessName">
{{Form::text('ProcessName', '', ['id'=>'ProcessName', 'class' => 'form-control', 'placeholder'=>''])}}
</td>
<td data-name="MachineName">
<div class="form-group row-fluid m-auto">
<select name="MachineName" class="form-control" id="MachineName" data-live-search="true">
<option value=""></option>
@foreach($machineName as $machineName)
<option value="{{$machineName->id}}">{{$machineName->MachineName}}</option>
@endforeach
</select>
</div>
</td>
<td data-name="MachineQty">
{{Form::number('MachineQty', '', ['id'=>'MachineQty', 'class' => 'form-control', 'placeholder'=>''])}}
</td>
<td data-name="SMV">
{{Form::number('SMV', '', ['id'=>'SMV', 'class' => 'form-control', 'placeholder'=>''])}}
</td>
<td data-name="del">
<a name="del0" id="del0" class="btn btn-outline-danger row-remove" value="del0">Delete</a>
{{-- <button name="del0" class='btn btn-outline-danger row-remove'>Delete</button> --}}
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align: left;">
<a id="add_row" class="btn float-right btn-lg btn-block btn-outline-secondary">Add Row</a>
</td>
</tr>
<tr>
<td colspan="5" style="text-align: left;">
{{Form::submit('Save', ['class'=>'btn btn-lg btn-outline-primary btn-block', 'name'=>'ProductInsert'])}}
</td>
</tr>
</tfoot>
</table>
有我的控制器
// Create Post
$work_breakdown = new work_breakdown;
$work_breakdown->Buyer = $request->input('Buyer');
$work_breakdown->Style = $request->input('Style');
$work_breakdown->Item = $request->input('Item');
$work_breakdown->Size = $request->input('Size');
$work_breakdown->ThreadType = $request['Thread'];
$work_breakdown->Description = $request->input('Description');
$work_breakdown->ProductImage = $fileNameToStore;
$work_breakdown->user_id = auth()->user()->id;
$work_breakdown->save();
$maxValue = work_breakdown::max('id');
$i = 0;
$ProcessName_ID = $request->input('MachineName');
foreach($ProcessName_ID as $key => $MachineName) {
$i++;
// Create Post
$work_breakdown = new work_breakdown;
$work_breakdown->ProductID = $maxValue;
$work_breakdown->MachineID = $MachineName;
$work_breakdown->ProcessName = $request->input('ProcessName');
$work_breakdown->MachineQty = $request['MachineQty'];
$work_breakdown->SMV = $request['SMV'];
$work_breakdown->user_id = auth()->user()->id;
$work_breakdown->save();
}
我发现了这个错误: "为 foreach() 提供的参数无效"
【问题讨论】:
-
这仅仅意味着你试图循环的东西(可能是
$ProcessName_ID)不是可以循环的东西。例如,你可能认为它是一个数组,但它实际上是一个字符串。 -
您需要验证输入是打印一个数组还是获取第一个值
-
@Alex Howansky,是的,实际上我是新手,我不知道如何从表中获取数据。你能帮我这样做吗?