How to Redirect to Previous Page in Laravel

When creating a web application, there will undoubtedly be times when you need to redirect a user to the previous page. Have you ever tried to submit a form on a website only to have it come back as invalid? It can be frustrating, especially if you’re not sure what you did wrong.

Rather than making the user try to figure it out, you can use the back helper function to redirect them to their previous location. This way, they can simply fix the problem and resubmit the form. Just keep in mind that since this feature uses the session, you’ll need to make sure the route is using the web middleware group or has all of the session middleware applied. Otherwise, it won’t work properly.

Laravel provides several ways to help developers do this easily.

In controller:

//several way to redirect
return back();
return back()->withInput();
return redirect()->back(); 

In view:

{{ url()->previous() }}
{{ Form::hidden('url', URL::previous()) }}

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close