我一直在尝试通过刀片添加多个数据。在我的刀片形式中,我使用克隆,添加行。例如,一份请购单有许多项目。
My blade view
刀片视图代码
<form autocomplete="off" method="POST" action="{{ route("requisition.store") }}" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label class="required" for="purpose">Purpose</label>
<input class="form-control" type="text" name="purpose" id="purpose" required>
<span class="help-block"></span>
</div>
<div class="form-group">
<label class="required" for="requisition_date">Date</label>
<input class="form-control" type="date" name="requisition_date" id="requisition_date" required>
<span class="help-block"></span>
</div>
<div class="form-group">
<label class="required" for="requested_by">Released By</label>
<input class="form-control" type="text" name="requested_by" id="requested_by" required>
<span class="help-block"></span>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>Qty</th>
<th>Unit</th>
<th>Description</th>
<th>
<a href="#" class="addRow"><i class="fas fa-plus"></i></a>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="number" name="rows[0][qty]" class="form-control quantity" required>
</td>
<td>
<input type="text" name="rows[0][unit]" class="form-control" required>
</td>
<td>
<input type="text" name="rows[0][description]" class="form-control" required>
</td>
<td>
</td>
</tr>
</tbody>
</table>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</form>
用于克隆的脚本代码
function addRow()
{
var tr = '<tr>'+
'<td><input type="number" name="rows[0][qty]" class="form-control quantity"></td>'+
'<td><input type="text" name="rows[0][unit]" class="form-control quantity"></td>'+
'<td><input type="text" name="rows[0][description]" class="form-control quantity"></td>'+
'<td><a class="btn btn-danger remove"><i class="fas fa-times"></i></a></td>'+
'</tr>';
$('tbody').append(tr);
}
和我的商店控制器代码
public function store(Request $request)
{
$requisition = Requisition::create([
'purpose' => $request->input('purpose'),
'requisition_date' => $request->input('requisition_date'),
'requested_by' => $request->input('requested_by')
]);
$rows = $request->input('rows');
foreach ($rows as $row)
{
$items[] = [
'requisition_id' => $requisition->id,
'qty' => $row['qty'],
'unit' => $row['unit'],
'description' => $row['description']
];
}
RequisitionItem::insert($items);
}
我的商店控制器代码中的问题是它只添加一项而不是多项。
我的模型:
申请表 = 'id', '目的', 'requisition_date', 'requested_by', 'requisition_date'
RequisitionItem 表 = 'id', 'requisition_id', '数量', '单位', '描述'
dd($rows) 结果