How to redirect after login in Laravel?

Member

by mac , in category: PHP Frameworks , 9 months ago

How to redirect after login in Laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elisha_langworth , 3 months ago

@mac 

To redirect a user after login in Laravel, you can use the redirect helper function. Here's an example of how to use it:

1
2
3
4
if (Auth::attempt($credentials)) {
    // Authentication passed...
    return redirect()->intended('dashboard');
}


In this example, the user will be redirected to the dashboard route if the login credentials are valid. If you want to redirect the user to a different route, simply replace dashboard with the desired route name.


If you want to redirect the user to a specific URL instead of a route, you can use the to method:

1
return redirect()->to('https://example.com');


You can also pass a second argument to the to method to specify the status code for the redirect. For example:

1
return redirect()->to('https://example.com', 302);


This will send a 302 (Found) status code with the redirect.