How do I post in two tables using one form in Laravel?
I have two tables, students and parents, I created the models and linked them. I created the controllers for both tables as well.
I have a ‘create student’ form where I accept student name and parent name but I want to send students name to students table and parents name to parents table which is joined by the student_id field. Any idea how I can achieve this ?
You create a Student instance from the student form inputs, then using that instance, through the relationship, create a Parent instance using the parent inputs:
// StudentController
public function store(Request $request)
{
$student = Student::create($request->input('student'));
$student->parent()->create($request->input('parent'));
}
You can group the student and parent inputs in your form, for example: