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

Lopsum's avatar

Retrieve Session after redirect->back() on validor fails

Hello everyone ! I've an old Laravel 5.4 version with a multistep form. On each step, I validate the step and store the values of the form on the session with a specific key. Nothing is stored on the database until the final step.

eg :

[  
  'form' => [  
     'input1' => 'value1',  
     'input2' => 'value2',  
     ...  
  ]  
 ]

The problem is when I validate the form and an error occur, I display a SweetAlert modal with the errors. It just the default redirect :

if ($validator->fails()) {  
  return redirect()  
     ->back()  
     ->withErrors($validator)  
     ->withInput();  
}

When I resubmit the form, as I retrieve the session when saving to the database, I get errors telling me that the session does not exist.

If I do a dd() of the session, yes, I no longer have the form-related session, but only the errorBag and the old values of the last step of the form.

I tried to put a Session::save() before the redirect (like I can see in this topic), but it doesn’t change anything.

Do you know how can I persist/retrieve the Session values after a redirect()->back() ? Thanks in advance for your help :)

0 likes
3 replies
Snapey's avatar

in your multi-step form, do you redirect to the next form or do you just return the next form at the end of the post request?

Lopsum's avatar

Hey, Thanks for your responses.

It's a collection of div for each step, with a form attached for each. For the submission, I use Ajax for sending a POST request and if everything is good, I hide the step to make the next one appear.

Here an example for one step in the controller and the process function (for edit and create) :

public function processStep2Form(RequestStep2Data $request) {
       $data = $request->except(['_token']);
        if($request->is('requests/*')) {
            if(!Session::get('edit.'.$request->id) == null) {
                Session::put('edit.'.$request->id, array_merge(Session::get('edit.'.$request->id), $data));
            } else {
                Session::put('edit.'.$request->id, $data);
            }
        } else {
            Session::put($data);
        }
        return response()->json();
    }

On the last step this is a regular POST request, without Ajax. After the validation, I store the last step values on the session (eg for the edit) :

Session::put('edit.'.$request->id, array_merge(Session::get('edit.'.$request->id), $data));

And store the values on DB with a repository :

$this->requestRepository->store(Session::all());

I've a Session with my values on each step after Ajax submission (with or without fails). But on the last step, if the validator fails, everything is gone after the `redirect()→back(). I hope it's clear.

Please or to participate in this conversation.