我正在尝试根据用户ID向数据库添加多行,但是当我提交时,它只添加了最后一行数据。
控制器
if (isset($_POST['save'])) {
$sub1 = Input::get('sub1');
$sub2 = Input::get('sub2');
$sub3 = Input::get('sub3');
$sub4 = Input::get('sub4');
$key = Input::get('recordid');
$req = new Results();
foreach ($key as $id) {
$req->st_id = $id;
foreach ($sub1 as $s1) {
$req->subject1 = $s1;
}
foreach ($sub2 as $s2) {
$req->subject2 = $s2;
}
foreach ($sub3 as $s3) {
$req->subject3 = $s3;
}
foreach ($sub4 as $s4) {
$req->subject4 = $s4;
}
$req->save();
}
我的观点
@foreach ($data as $row)
<tr>
<td>{{$row->id}}
<input name="recordid[]" id="recordid[]" value="{{ $row->id}}" type="hidden"></td>
<td>{{$row->fname}} {{$row->lname}}</td>
<td><input type="text" name="sub1[]" id="sub1[]" class="form-control" placeholder="Add marks here..."></td>
<td><input type="text" name="sub2[]" id="sub2[]" class="form-control" placeholder="Add marks here..."></td>
<td><input type="text" name="sub3[]" id="sub3[]" class="form-control" placeholder="Add marks here..."></td>
<td><input type="text" name="sub4[]" id="sub4[]" class="form-control" placeholder="Add marks here..."></td>
</tr>
@endforeach
...............
$req = array();
$sub1 = Input::get('sub1');
$sub2 = Input::get('sub2');
$sub3 = Input::get('sub3');
$sub4 = Input::get('sub4');
$key = Input::get('recordid');
foreach($key as $k=>$id){
$req[$k]['st_id'] = $id;
$req[$k]['subject1'] = $sub1[$k];
$req[$k]['subject2'] = $sub2[$k];
$req[$k]['subject3'] = $sub3[$k];
$req[$k]['subject4'] = $sub4[$k];
}
if(!empty($req)){
DB::table('table_name')->insert($req);
}