vikiedr
vikiedr

Reputation: 19

Redirect back to form after checking values in Laravel

I have a form that sends data via POST to a controller function which displays a view with overview of what the user put in. I this view user has 2 options - SAVE the data if they are OK or EDIT them.

Is there some way to redirect back with the input? I cant figure it out so Im using a form in the view with hidden input which carries the data a then via POST method send it to the display form function. But I don't think that is the right way there should be something simpler.

Thank you

Upvotes: 0

Views: 1784

Answers (4)

coleander
coleander

Reputation: 581

Assuming you are using Laravel 5.5

After reading the comments on Nisarg Shah's answer i think what i would do to not have the hidden form, is to save the data he first passes in a session and after the final save(After edit or pressing the save button) just remove the data from the session.

So after the first form we arrive at your controller method, in here we save the data to a session and shows the user a view where he can see the data, here you would print the data with {{ session('key') }} somewhere in the view for all the data.

The user can now choose to edit the data, the edit button would then go to a specific route and here you would return a view with a form he could edit, and after edit post to another route where you would just save the data as normal.

If he presses save on the "confirmation" view you would again make the button go to a specific url and in the controller method just retrieve the session data and save it to the database.

Upvotes: 1

Nisarg Shah
Nisarg Shah

Reputation: 292

So heres what your going to do:

  • Lets say the user wants to save the data and clicks okay, you can simply store this information into a database through a model or through query builder

    DB::table('usersinput')->insert([
       ['email' => 'taylor@example.com', 'name' => 'taylor'],
       ['email' => 'dayle@example.com', 'name' => 'dayle']
    ]);
    
  • Lets say the user clicks edit, than you can return the view with the values

    $email = "dayle@example.com";
    $name = "dayle";
    return view('welcome', compact('email', 'name'));
    
  • in the view you can set the input value to the data

    <form>
      email:<br>
      <input type="text" name="{{ $email }}"><br>
      name:<br>
      <input type="text" name="{{ $name }}">
    </form>
    

Upvotes: 0

Manoj H L
Manoj H L

Reputation: 843

You can use

back()->withInput();

or if you want to redirect to a another URL use

return Redirect::route('ROUTE.NAME')->withInput();

Upvotes: 0

Abid Raza
Abid Raza

Reputation: 755

Have you tried this..?

public function store()
{
   //.....    

   return redirect()->back()->withInput();  
}

Upvotes: 0

Related Questions