Laravel Redirect Back to Previous Page After Login

Published on June 29, 2020 27 sec read

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

  1. Solution 1
  2. Solution 2

Solution 1

The LoginController path is app/Http/Controllers/Auth/LoginController.php.

In the LoginController, add this showLoginForm() function:

public function showLoginForm()
        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->redirectTo = url()->previous();
That’s it. Thanks for reading. 🙂

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.