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

tkmitpl's avatar

Inertia multiple forms in same view

Hi guys!

I'm working on an application which has two related models

Models\Employee
     user_id: foreignIdFor(User::class)
     ...
*Models\User*
     name: string
     email: string
     ...

I want to be able to automatically create a User every time an Employee is created. For this, in my view I have created two forms, userForm and employeeForm using the Inertia form helper.

How do I get the id of the newly created user and pass it to the employeeForm before posting it. So far I have tried Redirect::back()->with(['user'=>$user]) but I don't seem to be getting the user object in the response that is received in the onSuccess callback of the userForm.

The reason I am trying to avoid using axios or fetch is mostly academic. If there's no elegant solution then that is what I'll be doing but I'm just curious as to how others are handling this problem.

0 likes
2 replies
aleahy's avatar

When you do

->with(['user' => $user])

You're creating a key/value pair on the session for this response. If you want to be able to access it on your page, then the controller method that returns the page will need to access the session variable and pass it as a prop to the page.

So your controller for displaying the form would need:

Inertia::render('Users/Employee', [
   'user' => session('user')
]);

And your page would need to define a prop for the user which would be filled with the user when the page is refreshed.

Please or to participate in this conversation.