'Change default route in Laravel

I have this code in RouteServiceProvider

  public const HOME = '/home';

i have overridden this using redirectTo method

    public static function redirectTo($guard)
    {
       if($somecondition)
        {
         return $guard . '/route1';
        }
        else
        {
          return $guard . '/route2';
         }
        
    }

but it stills calls home. Using laravel 8



Solution 1:[1]

You should change the handle method at app\Http\Middleware\RedirectIfAuthenticated.php

public function handle(Request $request, Closure $next, ...$guards)
{
    $guards = empty($guards) ? [null] : $guards;

    foreach ($guards as $guard) {
        if (Auth::guard($guard)->check()) {
            if ($somecondition) {
                return $guard . '/route1';
            } else {
                return $guard . '/route2';
            }
        }
    }

    return $next($request);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shabeer Ahammed