Laravel 5 – After login redirect back to previous page

Solution for laravel 5.3:

In loginController overwrite the showLoginForm() function as this one:

public function showLoginForm()
{
    if(!session()->has('url.intended'))
    {
        session(['url.intended' => url()->previous()]);
    }
    return view('auth.login');    
}

It will set the “url.intended” session variable, that is the one that laravel uses to look for the page which you want to be redirected after the login, with the previous url.

It also checks if the variable has been set, in order to avoid the variable to be set with the login url if the user submit the form with an error.

Leave a Comment