Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hello_World's avatar

redirect to intended page after login - laravel 5.7

I have an e-commerce site. When user going to checkout page, they need to login to the system. After authentication, the page will redirect to checkout page. I used multi-table-auth in my e-commerce website (https://scotch.io/@sukelali/how-to-create-multi-table-authentication-in-laravel). I used redirect()->intended(), which is not working. I also searched for result in online, nothing seems to working for my situation. Can anyone help me out! Here is my code for login:

use AuthenticatesUsers;

protected $redirectTo = '/';

public function __construct()
{
	$this->middleware('guest')->except('logout');
}

public function guard()
{
	return Auth::guard('customer');
}


public function login(Request $request)
{
    $rememberMe = $request->remember ? true : false;
    $customer = Customer::where(['phone' => $request->phone, 'password' => $request->pin])->first();

    if (!empty($customer)) {
        Auth::guard('customer')->login($customer, $rememberMe);

        return redirect()->intended('landing_page');
    }

    return back()->withInput()->withErrors(['Invalid Credential!']);
}
0 likes
12 replies
martinbean's avatar
Level 80

@nasirnobin You need to set the intended URL before redirecting to login.

$request->session()->put('url.intended', url('/intended-url'));
5 likes
RoboRobok's avatar

@MARTINBEAN - I don’t think you ever need to put url.intended to session manually. That’s exactly what intended() method does.

Hello_World's avatar

@ROBOROBOK - intended() is not working here. I think, it is redirecting me to login page before hitting the route. Here is my route:

Route::group(['middleware'=>'customer'], function() {
    Route::get('/cart_items', 'CartController@index')->name('cart_items');
});
Hello_World's avatar

@MARTINBEAN - I don't wanna be specific. Rather than i want it to be dynamic. I created new project and tried intended() with some basic routing. which is working fine. but when i'm using custom auth, its not working anymore. So i guess it's something to do with middleware, But i can't figured it out.

munazzil's avatar

Remove the below code and check because it is run before the intended,

    //protected $redirectTo = '/';
grenadecx's avatar

He could call the proper function that deals setting it instead of setting the session manually.

redirect()->setIntendedUrl(url('/intended-url'));
1 like
Lkm96's avatar

How did you get to solve it finally '

Hadayat's avatar
public function __construct()
    {
        $this->middleware('auth');
    }

Add this code on top of your controller, after successful, it redirects you on specific where you want to redirect which is define in your route request.

Please or to participate in this conversation.