skip to Main Content

only fist two line execute in this code last two role not check how can solve this problem

$input = $request->all();
        $this->validate($request,[
            'email'=>'required|email',
            'password'=>'required'
        ]);
        if(auth()->attempt(['email'=>$input["email"], 'password'=>$input['password']]))
        {
            
            
            if(auth()->user()->role == 'accountadmin')
            {
                return redirect()->route('accountadmins');
            }

            else if(auth()->user()->role == 'maintainadmin')
            {
                return redirect()->route('maintainadmins');
            }

            if(auth()->user()->role == 'superadmin')
            {
                return redirect()->route('superadmins');
            }

            if(auth()->user()->role == 'subscriber')
            {
                return redirect()->route('subscriberss');
            }


            else
            {
                return redirect()->route('home');
            }
        }
        else
        {
            return redirect()
            ->route("login")
            ->with("error",'Incorrect email or password');
        }
if(auth()->user()->role == 'superadmin')
            {
                return redirect()->route('superadmins');
            }

            if(auth()->user()->role == 'subscriber')
            {
                return redirect()->route('subscriberss');
            }

without checking this role go next step. i have four login role using auth . but when i execute the only first two role check after that go next step . i try to else if else if but same problem

2

Answers


  1. Although its much better to use middlewares or route -> permission binding, but to simply answer your question you can do the following:

    $routes = [
        'accountadmins' => 'accountadmin',
        'maintainadmins' => 'maintainadmin',
        'superadmins' => 'superadmin',
        'subscribers' => 'subscriber'
    ];
    

    Using array_search:

    if (false !== $route = array_search(auth()->user()->role, $routes)) {
        return redirect()->route($route);
    } else {
        return redirect()->route('home');
    }
    

    Personally I recommend to use a permission package such as spatie/laravel-permission it is very well maintained and documented:

    1. https://spatie.be/docs/laravel-permission/v5/basic-usage/middleware#package-middleware

    2. https://github.com/spatie/laravel-permission

    Login or Signup to reply.
  2. You could use a switch control structure here. Like this:

    switch (auth()->user()->role) {
        case 'accountadmin'  : return redirect()->route('accountadmins');
        case 'maintainadmin' : return redirect()->route('maintainadmins');
        case 'superadmin'    : return redirect()->route('superadmins');
        case 'subscriber'    : return redirect()->route('subscribers');
        default              : return redirect()->route('home');
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search