After logout redirect to login page in Laravel code example

Example 1: redirect after login laravel

class RegisterController extends Controller
{
    protected $redirectTo = '/home';

    protected function redirectTo()
    {
        if (auth()->user()->role_id == 1) {
            return '/admin';
        }
        return '/home';
    }

}

Example 2: how to redirect to another page after login in laravel

protected function authenticated(Request $request, $user) {
	 if ($user->role_id == 1) {
	 	return redirect('/admin');
	 } else if ($user->role_id == 2) {
	 	return redirect('/author');
	 } else {
	 	return redirect('/blog');
	 }
}

Example 3: laravel logout redirect

This is how I did it. In Auth\LoginController you have:

use AuthenticatesUsers;
Change it to:

use AuthenticatesUsers {
    logout as performLogout;
}
Then, define a new logout() method in your LoginController:

public function logout(Request $request)
{
    $this->performLogout($request);
    return redirect()->route('your_route');
}
Sure, regular logout() method in that trait has only 3 lines (used to log users out of the system) so you can copy them to your method, but you should always follow the DRY principle (don't repeat yourself) and re-use as much code as you can.

Example 4: how to redirect to another page after login in laravel

$this->redirectTo = route('dashboard');

Tags:

Php Example