ItSolutionStuff.com

Laravel Redirect Back to Previous Page After Login Example

By Hardik Savani • April 16, 2024
Laravel

Hello Guys,

This article will provide an example of laravel after login redirect back. if you want to see an example of laravel redirect to previous page after login then you are in the right place. step by step explain laravel redirect to previous route after login. I would like to show you how to redirect to previous page after login in laravel. Alright, let us dive into the details.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

In this tutorial, I will give you three ways to redirect back to the previous page after login in laravel application. so let's see one by one examples here:

Example 1: using redirect()->intended()

You can see below LoginController.php controller file. you need to update as like below:

app/Http/Controllers/Auth/LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use App\Providers\RouteServiceProvider;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

class LoginController extends Controller

{

/*

|--------------------------------------------------------------------------

| Login Controller

|--------------------------------------------------------------------------

|

| This controller handles authenticating users for the application and

| redirecting them to your home screen. The controller uses a trait

| to conveniently provide its functionality to your applications.

|

*/

use AuthenticatesUsers;

/**

* Where to redirect users after login.

*

* @var string

*/

protected $redirectTo = RouteServiceProvider::HOME;

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('guest')->except('logout');

}

public function showLoginForm()

{

return view("login");

}

public function login(Request $request)

{

$request->validate([

'email' => 'required',

'password' => 'required',

]);

$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials)) {

return redirect()->intended();

}

return redirect("login")->withSuccess('Oppes! You have entered invalid credentials');

}

}

Example 2: using Session

You can see below LoginController.php controller file. you need to update as like below:

app/Http/Controllers/Auth/LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use App\Providers\RouteServiceProvider;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

class LoginController extends Controller

{

/*

|--------------------------------------------------------------------------

| Login Controller

|--------------------------------------------------------------------------

|

| This controller handles authenticating users for the application and

| redirecting them to your home screen. The controller uses a trait

| to conveniently provide its functionality to your applications.

|

*/

use AuthenticatesUsers;

/**

* Where to redirect users after login.

*

* @var string

*/

protected $redirectTo = RouteServiceProvider::HOME;

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('guest')->except('logout');

}

public function showLoginForm()

{

if(!session()->has('url.intended'))

{

session(['url.intended' => url()->previous()]);

}

return view("login");

}

public function login(Request $request)

{

$request->validate([

'email' => 'required',

'password' => 'required',

]);

$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials)) {

return redirect(session()->get('url.intended'));

}

return redirect("login")->withSuccess('Oppes! You have entered invalid credentials');

}

}

Example 3: using redirectTo variable

You can see below LoginController.php controller file. you need to update as like below:

app/Http/Controllers/Auth/LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use App\Providers\RouteServiceProvider;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller

{

/*

|--------------------------------------------------------------------------

| Login Controller

|--------------------------------------------------------------------------

|

| This controller handles authenticating users for the application and

| redirecting them to your home screen. The controller uses a trait

| to conveniently provide its functionality to your applications.

|

*/

use AuthenticatesUsers;

/**

* Where to redirect users after login.

*

* @var string

*/

protected $redirectTo = RouteServiceProvider::HOME;

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('guest')->except('logout');

$this->redirectTo = url()->previous();

}

}

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to use Laravel Variable in JQuery?

Read Now →

Laravel Cashier Stripe Subscription Example Tutorial

Read Now →

How to Use Google Translator in Laravel?

Read Now →

Laravel Ajax DELETE Request Example Tutorial

Read Now →

Laravel React JS Pagination using Vite Example

Read Now →

How to Store Array in Database Laravel?

Read Now →

How to Get All Session Data in Laravel?

Read Now →

Laravel 9 REST API Authentication using Sanctum Tutorial

Read Now →

Laravel Two Factor Authentication using Email Tutorial

Read Now →

Laravel Sanctum SPA API Authentication Example

Read Now →

How to Increase Session Lifetime in Laravel?

Read Now →