Skip to content
Advertisement

Unable to retrieve data stored on Laravel session on subsequent requests

I am writing a programme that requires a multi-step form before all the validated data will be stored in the database. For instance, Users have to provide their personal information before proceeding to the next step, and I want the validated data to be stored as a session up till the user completed its registration before all the data will be stored in a database.

Here is my code so far:

   if($validated == true){
       session(['reg_user' =>[
        'firstname' => $request->firstname,
        'lastname' => $request->lastname,
        'dob' => $request->dob,
        'email' => $request->email,
        'phone' => $request->phone,
        'title' => $request->title,
        'country' => $request->country,
        'state' => $request->state,
        'city' => $request->city,
        'zipcode' => $request->zipcode,
        'address' => $request->address,
       ]]);
   }

The session is only accessible on the next request but not on subsequent requests

Advertisement

Answer

First, make an array of data.

 $validates =>[

            'firstname' => $request->firstname,
            'lastname' => $request->lastname,
            'dob' => $request->dob,
            'email' => $request->email,
            'phone' => $request->phone,
            'title' => $request->title,
            'country' => $request->country,
            'state' => $request->state,
            'city' => $request->city,
            'zipcode' => $request->zipcode,
            'address' => $request->address
    ];

Then finally check the validation and put the data into a session with key and value.

  if($validated == true){
  $requestUser = collect($validates); 
  Session()->put('requestUser', $requestUser);
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement