In default Laravel 11, there's no hard-coded redirect value.
Laravel itself checks in the Middleware, which is fired if the user is already logged in if Routes with the name dashboard or home exist. If one exists, the user will be redirected to that route. Otherwise, Laravel redirects to the root.
src/Illuminate/Auth/Middleware/RedirectIfAuthenticated.php:
class RedirectIfAuthenticated{ // ... protected function defaultRedirectUri(): string { foreach (['dashboard', 'home'] as $uri) { if (Route::has($uri)) { return route($uri); } } $routes = Route::getRoutes()->get('GET'); foreach (['dashboard', 'home'] as $uri) { if (isset($routes[$uri])) { return '/'.$uri; } } return '/'; }}
So, first, you need to have your custom logic.
Let's say that your logic will be inside of your User model, like this:
app/Models/User.php:
class User extends Authenticatable{ // ... public function getRedirectRoute() { return match((int)$this->role_id) { 1 => 'student.dashboard', 2 => 'teacher.dashboard', // ... }; }}
Then, you must create your Middleware with the logic and assign the same guest, in this case, an alias.
php artisan make:middleware OnlyGuestAllowedMiddleware
app/Middleware/OnlyGuestAllowedMiddleware.php:
use Closure;use Illuminate\Http\Request;use Illuminate\Support\Facades\Auth;use Symfony\Component\HttpFoundation\Response; class OnlyGuestAllowedMiddleware{ public function handle(Request $request, Closure $next, string ...$guards): Response { $guards = empty($guards) ? [null] : $guards; foreach ($guards as $guard) { if (Auth::guard($guard)->check()) { return redirect(Auth::user()->getRedirectRoute()); } } return $next($request); }}
bootstrap/app.php:
return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { $middleware->alias([ 'guest' => \App\Http\Middleware\OnlyGuestAllowedMiddleware::class ]); }) ->withExceptions(function (Exceptions $exceptions) { // })->create();
Laravel Breeze
In Laravel Breeze, you must change in two places separately: Controller for the Registration and Login.
app/Http/Controllers/Auth/RegisteredUserController.php:
public function store(Request $request): RedirectResponse{ // ... Auth::login($user); return redirect(route('dashboard', absolute: false));}
Change the last line to:
return redirect(auth()->user()->getRedirectRoute());
And then the Login Controller.
app/Http/Controllers/Auth/AuthenticatedSessionController.php:
public function store(LoginRequest $request): RedirectResponse{ $request->authenticate(); $request->session()->regenerate(); return redirect()->intended(route('dashboard', absolute: false));}
Change the last line to:
return redirect()->intended(auth()->user()->getRedirectRoute());
You may also be interested in our related PREMIUM course: Laravel 11: Breeze with User Role Areas
I applied your code in breeze but it gives me Unhandled match case 0
Match case? I didn't use any
matchoperation in this example. Maybe your error is in some other parts? Can't really comment without looking at the whole code of your project.thank you for your reply You use it in
but the problem happens when register so the error appear to me
Oh right, sorry, missed that part. Well, that means that during registration you don't assign
role_idcorrectly and it's set to 0?