vendredi 21 juillet 2017

Laravel5.1 user_id insert issue in multiple insertion of records

I need to insert student_id into a table(exam_marks). Data is from a table where a user filters students according to various categories then can assign them marks respectively. When i currently submit... the student_id on exam_marks table gets populated with default array key[0,1,2....] as id's

                       ** #Adding marks view:#** 

         <table id="students" class="table table-striped table-bordered datatable">
                                            <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>photo</th>
                                                <th>Student Name</th>
                                                <th>Reg No</th>
                                                <th>Exam Mark</th>
                                                <th>Action</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            @foreach($students as $student)
                                                <tr>
                                                    <td></td>
                                                    <td><img height="60" width="60" src="" alt="" id="imagePreview"></td>
                                                    <td> </td>
                                                    <td></td>
                                                    <td></td>
                                                    <td class="form-group">
                                                        <div>
                                                            {!! Form::text("examMark[]",null,['class'=>'form-control spinner_default'])!!}
                                                        </div>
                                                    </td>
                                                </tr>
                                                
                                            @endforeach
                                            </tbody>
    </table>
    [marks table][1]

                   **#Marks Controller:#**

    public function store(Request $request)
        {
          //dd($request);
            $this->validate($request, $this->rules);

            $stage_id   = Input::get('stage_id');
            $stream_id  = Input::get('stream_id');
            $year_id    = Input::get('year_id');
            $exam_id    = Input::get('exam_id');
            $subject_id = Input::get('subject_id');
            $term_id    = Input::get('term_id');

            $examMarksList = array();
            $examMarks = Exam_mark::where('exam_id',$exam_id)->where('class_id',$stage_id)->where('subject_id',$subject_id)
                ->where('stream_id',$stream_id)->where('term_id',$term_id)->where('year_id',$year_id)->get();
            foreach ($examMarks as $stMark) {
                $examMarksList[$stMark->student_id] = array("examMark"=>$stMark->examMark);
            }

            $mark = (Input::get('examMark'));
            while (list($key, $value) = each($mark)) {
    //            print "$key is $value\n";
                if(!isset($examMarksList[$key])){
                    $examMarks = new Exam_mark();
                    $examMarks->exam_id = $exam_id;
                    $examMarks->class_id = $stage_id;
                    $examMarks->subject_id = $subject_id;
                    $examMarks->stream_id = $stream_id;
                    $examMarks->term_id = $term_id;
                    $examMarks->year_id = $year_id;
                    $examMarks->student_id = $key;
                    $examMarks->examMark = $value;
                    $examMarks->save();
                }else{
                    $examMarks = Exam_mark::where('exam_id',$exam_id)->where('class_id',$stage_id)
                        ->where('subject_id',$subject_id)->where('stream_id',$stream_id)->where('term_id',$term_id)
                        ->where('year_id',$year_id)->where('student_id',$key)->first();
                    $examMarks->examMark = $mark[$key];
                    $examMarks->save();
                }
            }
        }


    [i want to populate this table with the right student_ids][2]

                **Captions**
      [1]: http://ift.tt/2twXSYb
      [2]: http://ift.tt/2tNIZMj



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire