Laravel 7 Redirect to Previous Page After Login Example

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();
}

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *