如何在laravel上插入一组文本值

时间:2017-11-28 13:48:42

标签: php mysql laravel-5.3

我尝试插入一组文本值,但它只在数据库中存储最后一个文本值。 如何通过单击存储所有文本值插入...

group of text box- picture

inserting code- picture

database- picture

1 个答案:

答案 0 :(得分:0)

HTML CODE:

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

    }