我正在尝试将多个子项添加到我的雄辩模型作为数组,但我无法这样做。
查看:
<div class="row">
<div class="col-md-6">
<form class="" action="{{route('production.update', $rm->id)}}" method="post">
<input name="_method" type="hidden" value="PATCH">
{{csrf_field()}}
<!-- <input type="hidden" name="product_id" id="product_id" class="form-control" value="{{$rm->id}}"> -->
<div class="form-group{{ ($errors->has('batch_id')) ? $errors->first('title') : '' }}">
<lable>Batch ID</lable>
<input type="text" name="batch_id" id="batch_id" class="form-control" value="{{$rm->product_id}}" readonly="">
{!! $errors->first('wastage','<p class="help-block">:message</p>') !!}
</div>
<div class="form-group{{ ($errors->has('item_id')) ? $errors->first('title') : '' }}">
<lable>Item Name</lable>
<input type="hidden" name="item_id" id="item_id" class="form-control" value="{{$rm->item_id}}" readonly="">
<input type="text" class="form-control" value="{{$rm->item->item_name}}" readonly="">
{!! $errors->first('wastage','<p class="help-block">:message</p>') !!}
</div>
<div class="form-group{{ ($errors->has('rquantity')) ? $errors->first('title') : '' }}">
<lable>Quantity (Kg)</lable>
<input type="text" name="rquantity" id="rquantity" class="form-control" value="{{$rm->quantity}}" readonly="">
{!! $errors->first('wastage','<p class="help-block">:message</p>') !!}
</div>
<div class="form-group{{ ($errors->has('')) ? $errors->first('title') : '' }}">
<lable>Packing</lable>
@foreach($subitem as $sub)
<div class="row">
<div class="col-md-4">
<input type="checkbox" name="subitem[{{$sub->id}}]['subitem_id']" value="{{$sub->id}}"> {{$sub->subitem_name}}<br><br>
</div>
<div class="col-md-4">
<input type="text" name="subitem[{{$sub->id}}]['qty']" id="qty" class="form-control" placeholder="Entire Qty">
{!! $errors->first('qty','<p class="help-block">:message</p>') !!}
</div>
</div>
@endforeach
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Add">
</div>
</form>
</div>
</div>
控制器:
public function update(Request $request, $id)
{
$result = Production::findOrFail($id);
foreach($request->subitem as $key=>$row)
{
//print_r($request->subitem);exit;
$items = new Itempacking;
$items->batch_id = $result->product_id;
$items->item_id = $result->item_id;
$items->rquantity = $result->quantity;
$items->product_id = $result->id;
$items->subitem_id = $row['subitem_id'];
$items->qty = $row['qty'];
$items->status = 1;
$items->save();
$items='';
}
}
当我取消注释此行print_r($request->subitem); exit;
时,我可以这样看:
Array (
[3] => Array (
['subitem_id'] => 3
['qty'] => 2
)
[4] => Array (
['subitem_id'] => 4
['qty'] => 3
)
)
但是当我评论并尝试将数据发送到表格时,它无效。它给出了这条消息
未定义的索引:subitem_id
答案 0 :(得分:1)
不要插入&#39;&#39;对于HTML代码中的数组键。您可以将您的请求打印到详细信息。
替换
subitem[{{$sub->id}}]['subitem_id']
subitem[{{$sub->id}}]['qty']
到
subitem[{{$sub->id}}][subitem_id]
subitem[{{$sub->id}}][qty]