In this tutorial you will learn about the Laravel 7 Redirect to Previous Page After Login Example and its application with practical example.
In this laravel tutorial, I’ll show you how to redirect back to previous page after login in laravel project. In this tutorial you will learn to redirect user back to the previous page or url after login in laravel.
Laravel 7 Redirect to Previous Page After Login Example
In this article I’ll you show you two different methods to redirect user back to previous page after login.
First Method : How to redirect user Previous Page After Login
Open your loginContorller.php file then add this showLoginForm()
method as following:
1 2 3 4 5 6 7 8 |
public function showLoginForm() { if(!session()->has('url.intended')) { session(['url.intended' => url()->previous()]); } return view('auth.login'); } |
In this function we will be setting the “url.intended” session variable. Laravel uses this variable to check for the page to which user will be redirected after login.
Second Method : Laravel redirect user Previous Page After Login
In this method, you have to put the following code in LoginController’s __construct()
method as follow:
1 |
$this->redirectTo = url()->previous(); |
As follow:
1 2 3 4 5 |
public function __construct() { $this->middleware('guest')->except('logout'); $this->redirectTo = url()->previous(); } |