我尝试插入一组文本值,但它仅将最后一个文本值存储在数据库中。
我如何一键存储所有文本值插入...

最佳答案

HTML代码:

<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">{{$data->s_name}}</td>

        @if(!empty ($data->group) )
            <td class="text-center">{{ $data->group}}</td>
        @elseif(!empty ($data->trade) )
            <td class="text-center">{{ $data->trade}}</td>
        @elseif(!empty ($data->department) )
            <td class="text-center">{{ $data->department}}</td>
        @elseif(!empty ($data->section) )
            <td class="text-center">{{ $data->section}}</td>
        @else <td class="text-center">No Section</td>
        @endif

    <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>
</tr>
<input type="hidden" name="student_id" value="{{$data->student_id}}" >
<input type="hidden" name="sub_name" value="{{$subject }}">


在控制器中使用:

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);

    }

关于php - 我如何在Laravel上插入一组文本值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47533170/

10-09 23:04