添加在laravel 5.4
问题描述:
管理员角色我有一个数据库,在此:管理员拥有真isAdmin属性,但其他用户有假isAdmin财产。添加在laravel 5.4
我想检查谁登录的用户是管理员或没有通过它们重定向到我的应用程序不同的页面。我在Controller中的代码是:
public function store(User $user)
{
if (auth()->attempt(request(['email', 'password']))) {
if ($user->isAdmin == 1) {
return redirect('/ShowUser');
}
{
return redirect('/lo');
}
}
return back()->withErrors(
[
'message' => 'Error'
]
);
}
但是这段代码不起作用;它始终将用户发送到'/lo'
。我该如何解决它?
答
您错过了else
关键字。
就在这里:
if ($user->isAdmin == 1) {
return redirect('/ShowUser');
}
{ // <-- right here
return redirect('/lo');
}
添加else
关键字。
if ($user->isAdmin == 1) {
return redirect('/ShowUser');
}
else { // <-- right here
return redirect('/lo');
}
无论如何,即使在上面的编辑之后,您的代码仍然可以正常运行。但我有问题给你:
是否假设
user
在数据库中?数据库中
isAdmin
的默认值是多少?你是否将
isAdmin
属性作为输入从表单或其他东西传入?为什么它是一个
store
请求时,你只是想日志用户在?
这有点令人困惑。我可以从你的代码中知道你正在尝试登录一个用户,但是你正在使用store
方法(没有什么不对,只是约定),store
方法通常用于存储数据(如何巧合!)
'if($ user-> isAdmin){// Code here ...}'? – ako
对于** Laravel **中的**用户角色许可**管理**我建议[this](https://github.com/Zizaco/entrust)。 – ako
@ako此代码检查用户是否是数据库中的Admin属性 – Honey