Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vipin93's avatar
Level 13

Multiple form field saving problem?

I tried to save multiple field but problem with that its not saving student Id it's only look last id of student table. here is my controller

 public function section_student_post(Request $r)
    {
        $this->validate($r,[

            'section'        =>      'required'
        ]);
        $students = Student::where('user_id',Auth::user()->owner->id)
                             ->where('course_id',1)
                             ->where('active',1)
                             ->select('id')
                             ->get();                           
         $datetime = Carbon::now();            
         foreach ($r->section as $key => $value)
       {    
         $i = 1; 
         foreach ($students as $student) {
           $id=$student->id; 
         }
        $data = [
                 'section_id' => $value,
                 'student_id' => $id ,//here its skiing all student id only saving in al row with same student id
                 'roll_no'    => $i++,
                 'updated_at' => $datetime,
                 'created_at' => $datetime
          ];  
          StudentAcadmic::create($data);
      }   
       flash()->success('Successfully students section assined');
       return back();                            

    }

my view is here

<form method="post" action="/staff/section_student/attach" data-parsley-validate ="">
        {{ csrf_field() }}
            
                            <?php $i = 0 ?>
                            @foreach($students as $student)
                             <?php $i++ ?>
                             <tr>
                                <td>{{ $i }}</td>
                                <td>
                                    <a href="/st/student/{{$student->reg_no}}">{{ $student->reg_no }}
                                    </a>
                                </td>
                                <td>     
                                    {{ $student->name }} 
                                </td>
                                <td>
                                  <div class="form-group">
                                     <select class="form-control" name="section[]" id="section[]" required="">
                                        @foreach($student->courses->sections as $section)
                                          <option value="{{ $section->id }}">{{ $section->name }}</option>
                                        @endforeach
                                     </select>
                                  </div>
                                </td>
                             </tr>
                            @endforeach 
                           </tbody>
                        </table>
                        <div class="col-md-4 col-md-offset-4">
                            <button type="submit" class="btn-primary btn btn-block">Submit</button>
                        </div>
                   
           </form>

0 likes
1 reply
vipin93's avatar
Level 13

i solved the problem but here anyone how to increment no , like i have column roll_no so when i insert the value it automatically increase the no like 1, 2, 3, ...

Please or to participate in this conversation.