我有两个表,一个表称为标题,另一个表称为项目详细信息。 日期,供应商名称,票据编号等字段将转到表头表,项目名称,数量和单价等字段将进入项目明细表。
现在问题出在项目表中,我们有带有5个输入字段的表单,用户将在表单中输入项目详细信息并提交,但只保存了一个项目。我知道我们需要使用数组来处理,但是我不知道该如何编写代码。任何人都可以让我吗?
<tr>
<td class="text-center">1</td>
<td ><input type="text" class="form-control text-uppercase" name = "item_name[]" id="row1"></td>
<td ><input class="form-control text-center" type="text" onkeypress="return isNumberKey(event)" name="qty" id="qty1" onkeyup="calc1()" value=""></td>
<td ><input class="form-control text-right" type="text" onkeypress="return isNumberKey(event)" name="unit_price" id="price1" onkeyup="calc1()" value=""></td>
<td > <input class="form-control text-right" type="text" name="amount" id="amount1" value="" disabled></td>
</tr>
<tr>
<td class="text-center">2</td>
<td ><input type="text" class="form-control text-uppercase" name = "item_name[]" id="row2"></td>
<td ><input class="form-control text-center" type="text" onkeypress="return isNumberKey(event)" name="qty" id="qty2" onkeyup="calc2()" value=""></td>
<td ><input class="form-control text-right" type="text" onkeypress="return isNumberKey(event)" name="unit_price" id="price2" onkeyup="calc2()" value=""></td>
<td > <input class="form-control text-right" type="text" name="amount" id="amount2" value="" disabled></td>
</tr>
<tr>
<td class="text-center">3</td>
<td ><input type="text" class="form-control text-uppercase" name = "item_name[]" id="row3"></td>
<td ><input class="form-control text-center" type="text" onkeypress="return isNumberKey(event)" name="qty" id="qty3" onkeyup="calc3()" value=""></td>
<td ><input class="form-control text-right" type="text" onkeypress="return isNumberKey(event)" name="unit_price" id="price3" onkeyup="calc3()" value=""></td>
<td > <input class="form-control text-right" type="text" name="amount" id="amount3" value="" disabled></td>
</tr>
<tr>
<td class="text-center"></td>
<td class="text-right" colspan="3"><label>Total</label></td>
<td><input type="text" class="form-control text-right" onkeyup="bill_total()" name = "total" id="total" disabled></td>
</tr>
enter code here
公共功能商店(请求$ request,帐户$ account,项目$ item) {
if($request->attach->getClientOriginalName())
{
$ext = $request->attach->getClientOriginalExtension();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->attach->storeAs('public/categories',$file);
}
else
{
$file='';
}
$account->attach = $file;
$account->supp_name = $request->supp_name;
$account->emp_name = $request->emp_name;
$account->item_type = $request->item_type;
$account->bill_date = $request->bill_date;
$account->bill_amt = $request->bill_amt;
$account->bill_no = $request->bill_no;
$account->pay_mode = $request->pay_mode;
$account->purpose = $request->purpose;
$account->item_type = $request->item_type;
$account->save();
$item->item_name = $request->item_name;
$item->qty = $request->qty;
$item->unit_price = $request->unit_price;
$item->save();
return redirect('admin/accounts');
}
答案 0 :(得分:0)
首先,您应该将表列的类型更改为array。
在模型中添加类似的内容(将options
单词更改为列名):
protected $casts = [
'options' => 'array',
];
在您看来,输入字段的名称也必须是数组。像这样:
<input type="text" name="item[]">
(输入任意数量的输入)