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

JOHNMAC's avatar

save data from one form into two db tables

hello, save data from one form into two db tables is not working, according to the view it insert teacher name in teachers table and multiple students names with their teacher id in students table, how to done it using eloquent ORM, any guidline or solution? my view https://imgur.com/IjGKfLJ

controller file

public function store(Request $request)
{
    $request->validate([
        'teacher_name'    =>  'required',
        'student_name'    =>  'required',
    ]);

     $teacher = new Teacher;
     $teacher->teacher_name = $request->teacher_name;
     // $teacher->student()->attach($request->get('student_id'));
     // $teacher->student()->createMany([[$student1],[$student2]]);
     $teacher->save();

     $student = new Student;
     $student->student_name = $request->student_name;
     $student->save();

     return redirect('teacher');
}

blade file

  <form method="POST" action="{{ route('teacher.store') }}">
  @csrf
  <div class="form-group">
    <label for="">Teacher Name</label>
    <input type="text" name="teacher_name" class="form-control input {{ $errors->has('teacher_name') ? 'is-danger' : ''}}" id="" placeholder="Teacher Name">
  </div>

  <div class="form-group">
    <label class="text-right">Enter Student Name</label>
    <div class="col-md-8">
      <div class="wrapper">
        <div><input type="text" name="student_name[]" class="form-control input-lg" placeholder="Enter Student Name" /></div>
        </div>
        <p><button class="add_fields">Add More Students</button></p>
    </div>
  </div>

  <input type="submit" name="submit" class="btn btn-primary" value="submit">

  @include('errors')

</form>

teacher model

  protected $fillable = [
    'teacher_name',
  ];

  public function student()
  {
    $this->hasMany('App\Student');
  }

student model

  protected $fillable = [
     'student_name', 'teacher_id',
   ];

  public function teacher()
  {
      return $this->belongsTo('App\Teacher');
  }
0 likes
3 replies
wildwestriverrider's avatar

You are passing in an array of student names but not treating it as such in the controller.

Try looping though the student names.

foreach ($request->student_name as $name) {
// save student here
}
Snapey's avatar
Snapey
Best Answer
Level 122

the validation won't work as it is because you are passing an array.

You need to loop over the array

You need to associate each student with a teacher

public function store(Request $request)
{
    $request->validate([
        'teacher_name'    =>  'required',
        'student_name.*'    =>  'required',
    ]);

     $teacher = new Teacher;
     $teacher->teacher_name = $request->teacher_name;
     $teacher->save();

     foreach($request->student_name as $name) {

         $student = new Student;
         $student->teacher_id = $teacher->id;
         $student->student_name = $name;
         $student->save();
    }

     return redirect('teacher');
}

Or some would prefer that you are not so aware of how teacher and student are related, and instead;

     foreach($request->student_name as $name) {

         $teacher->student()->create([
             'student_name' => $name,
         ]);
    }

Please or to participate in this conversation.