Passing id across multiple forms to be stored as foreign key
I have a 4 step registration process (4 forms). Data gets stored in 3 tables. Step 1 & 2 share a table. Each table is linked using the (step 1) user table id column.
Currently I have 4 controllers
Step 1: Laravels default auth controller
Step 2: Data saves to the user the user table so I have to update the table. I created these functions
public function getUser() {
$id = $this->getId();
$user = User::findOrFail($id);
return $user;
}
public function getId() {
if (Auth::check()) {
$id = Auth::user()->id;
return $id;
}
}
public function store(Request $request)
{
$user = $this->findUser();
$input = $request->all();
$user->firstName = $data['firstName'];
$user->middleName = $data['middleName'];
$user->lastName = $data['lastName'];
$user->step = "2"; // increment step. when user logs in this column is checked and user is taken to whatever step they are on.
if ($user->save()) {
return view('auth/step3');
}
}
Controller 3 & 4: No record is created in these tables until the user submits the form but I still have the getId() function from above in both of these controllers to store the id because its a foreign key.
The store method in each controller (2,3,4) redirects to the next view (i.e. 2 redirects to 3) after successful save.
I am new to Laravel and I feel like there is a better way to accomplish this. I feel like the getId functions in each controller are repetitive. Can I accomplish the same thing through routing? Later on I plan on allowing users to continue off on whatever form they left off on. So if the user completes step 1, step 2 but logs out before completing step 3, when they log back in they will be taken back to the 3rd form.
In my User.php file for each step
public function step2()
{
return $this->hasOne('App\Step2', 'id', 'step2Id');
}
Please or to participate in this conversation.