I have Certificate and Student models, with one to many relation.
When the user is done making the certificate he is redirected to another page with student form, I dont know how to get the certificate_id or how to relate the student to the last certificate that it was created. help please.
Certificate Controller
public function store(StoreCertificateRequest $request)
{
$data = $request->validated();
$data['user_id'] = auth()->id();
Certificate::create($data);
return redirect()->route('students.create')->with(['success' => 'Certificate has been Saved']);
}
Student Controller
public function store(StoreStudentRequest $request)
{
$data = $request->validated();
Student::create($data);
return redirect()->route('users.index')->with('success', 'Students were added');
}
CodePudding user response:
So the certificate is already created, you can get that id by assigning a variable to the create, and then return it to the view as $variable->id