我试图从表单中一次插入几条记录 This is how the form appears
<label>Drug</label>
<select class="form-inline input-sm " name="drug[]" id="drug">
@foreach($drugs as $key => $drug)
<option value="{{$drug->id}}"> {{$drug->name}}</option>
@endforeach
</select>
<input class="input-sm" type="number" class="form-control" id="amount_received" name="amount_received[]" placeholder="Amount received">
<button type="button" name="add" id="add">Add More</button>
<button type="submit" class="btn btn-primary"id="submit">Submit</button>
我的控制器
public function addNewStock()
{
$input = Input::all();
$insert = array();
foreach($input['drug'] as $key => $drug) {
$insert[$key]['drug'] = $drug;
}
foreach($input['amount_received'] as $item => $amount_received) {
$insert[$item]['amount_received'] = $amount_received;
}
Stock::insert($insert);
}
这是我得到的结果($ input)。这是在我提交两条记录之后,问题是它选择了最后一条记录并在数据库中保存了两次
array:3 [▼
"_token" => "Oz9cSvNuPmPPHKAw6uKbnmA5DIMBsxKsnubaliHt"
"drug" => array:2 [▼
0 => "1"
1 => "10"
]
"amount_received" => array:2 [▼
0 => "2"
1 => "4"
]
]
请帮忙。