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
Although its much better to use
middlewares
orroute -> permission binding
, but to simply answer your question you can do the following:Using array_search:
Personally I recommend to use a permission package such as
spatie/laravel-permission
it is very well maintained and documented:https://spatie.be/docs/laravel-permission/v5/basic-usage/middleware#package-middleware
https://github.com/spatie/laravel-permission
You could use a switch control structure here. Like this: