Skip to main content

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.