In this laravel tutorial, you will learn how to redirect back to previous page or url after login in laravel apps.
This tutorial provides you two methods to redirect user back to previous url or page after login in laravel apps.
Let’s see the following method:
1: First Method
Go to your loginContorller and open it. Then add this showLoginForm()
function code as follow:
public function showLoginForm()
{
if(!session()->has('url.intended'))
{
session(['url.intended' => url()->previous()]);
}
return view('auth.login');
}
This function will set the “url.intended” session variable. Laravel uses this variable to look for the page in which the user will be redirected after login.
2: Second Method
In second method, you can add the following code into the add this line to the __construct()
function inside LoginController as follow:
$this->redirectTo = url()->previous();
As follow:
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->redirectTo = url()->previous();
}