如何一次插入多行laravel 5.3

时间:2017-12-09 14:55:16

标签: php laravel laravel-5.3

我想插入一个id为:blade.php的数组 enter image description here

<tr>
<td class="text-left">{{$count}}</td>
<td class="text-left"><img src="{{ asset('/' . $data->image_file) }}" class="img-circle" width="35px" height="35px"/></td>
<td class="text-left">{{$data->student_id}}</td>
<td class="text-left">
    {{ App\Models\student_subject:: where('id', '=', $subject)->value('sub_name') }}
</td>

<td class="text-left"> <input type="text" class="form-control"  placeholder="Mark" name="sub_mark[]" max="100" data-parsley-max="100" required="" data-parsley-required-message ="Subject Mark is required" data-parsley-trigger="change focusout"> </td>

<td class="text-left"> <input type="text" class="form-control" placeholder="Class Test Mark" name="ct_mark[]" max="20" data-parsley-max="20" required="" data-parsley-required-message ="CT Mark is required" data-parsley-trigger="change focusout"> </td>

你可以看到这里的所有代码 enter link description here

当我尝试提交表单时,它会插入具有相同ID的行。 enter image description here

这是我的控制器

public function PostAddResult(Request $request) {
$student_id= $request->input('student_id');
$sub_name= $request->input('sub_name');
$class_name= Session::get('class_name');
$exam_id= Session::get('exam_name');

$sub_mark= $request->input('sub_mark');
$ct_mark= $request->input('ct_mark');

 $i = 0;
 foreach($sub_mark as $marks){

    $student_res = new student_results();
    $student_res->student_id = $student_id;
    $student_res->class_name = $class_name;
    $student_res->sub_name = $sub_name;
    $student_res->sub_mark = $marks;
    $student_res->ct_mark = $ct_mark[$i];
    $student_res->exam_id = $exam_id;
    $student_res->save();

 }

 $notification = array(
            'message'=>'Student Marks Add Successfully',
            'alert-type'=>'success',
        );

return redirect('/addResult')->with($notification);

}

1 个答案:

答案 0 :(得分:0)

您希望为每条记录获取单独的学生ID和主题名称,因此请更改:

public class Robot extends Object{
private double energy;
private ArrayList<Object> attachments = new ArrayList<Object>();

public class Whisker extends Object{
private Object attached;
private Line whisker;

要:

<input type="hidden" name="student_id" value="{{ $data->student_id }}">
<input type="hidden" name="sub_name" value="{{ $subject }}">

然后,您将能够迭代标记,主题名称和学生ID:

<input type="hidden" name="student_id[]" value="{{ $data->student_id }}">
<input type="hidden" name="sub_name[]" value="{{ $subject }}">