Laravel Redirect Back to Previous Page After Login
In this article, I’m going to share to how redirect back to the previous page after login. Let’s see some solutions:
Table of Contents
Solution 1
The LoginController path is app/Http/Controllers/Auth/LoginController.php.
In the LoginController, add this showLoginForm()
function:
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.
Solution 2
Inside LoginController, add this line to the __construct()
function:
$this->redirectTo = url()->previous();
The full code will look like:
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->redirectTo = url()->previous();
}
That’s it. Thanks for reading. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)