how to do a switch statement in laravel fortify redirect after login

67 views Asked by At

how do i do a switch statement with

` ``

public function toResponse($request){return $request->wantsJson()
                ? response()->json(['two_factor' => false])

                : redirect()->intended(

                    auth()->user()->is_admin ? route('admin.dashboard') : route('dashboard')

                    auth()->user()->is_admin ? route('admin.dashboard') :route('dashboard'),
                                          
                    auth()->user()->is_Hr ? route('hr.dashboard') : route('dashboard'),

                    auth()->user()->is_clack ? route('clack.dashboard') : route('dashboard'),

                    auth()->user()->is_worker  ? route('workes.dashboard') : route('dashboard'),
                );
}`
``

want to redirect to each roles dashoard after login

redirect after login based on roles

1

There are 1 answers

1
Vaibhavraj Roham On

You need to create a new method or use a variable to get the intended route, you can't use how you added it.

Following should help you, Make sure user is loggedin at this point

public function toResponse($request) {
    return $request->wantsJson() ? 
        response()->json(['two_factor' => false]) : 
        redirect()->intended($this->getIntendedRoute());
}

public function getIntendedRoute() {
    return match(true) {
        auth()->user()->is_admin => route('admin.dashboard'),
        auth()->user()->is_Hr => route('hr.dashboard'),
        auth()->user()->is_clack => route('clack.dashboard'),
        auth()->user()->is_worker => route('workes.dashboard'),
        default => route('dashboard'),
    };

};
}

This code first checks each condition and returns the corresponding route if a match is found, and if none of the conditions match, it falls back to the default case and returns the route for the dashboard.