[英]Add Admin role in laravel 5.4
I have a database that in this: Admin has True isAdmin property, but other users have false isAdmin property. 我有一个数据库,其中:Admin具有True isAdmin属性,但其他用户具有false isAdmin属性。
I want to check if the user who logged in is an Admin or not by redirecting them to different pages in my app. 我想检查谁登录的用户是管理员或不将其重定向到我的应用程序不同的页面。 My code in Controller is: 我在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'
]
);
}
But this code doesn't work; 但是这段代码行不通; it sends the users to '/lo'
all the time. 它始终将用户发送到'/lo'
。 How can I fix it? 我该如何解决?
You're missing an else
keyword. 您缺少else
关键字。
Right here: 就在这儿:
if ($user->isAdmin == 1) {
return redirect('/ShowUser');
}
{ // <-- right here
return redirect('/lo');
}
add the else
keyword. 添加else
关键字。
if ($user->isAdmin == 1) {
return redirect('/ShowUser');
}
else { // <-- right here
return redirect('/lo');
}
anyway, your code will still run fine even after the edit above. 无论如何,即使经过上面的编辑,您的代码仍然可以正常运行。 But I have questions for you: 但是我有个问题要问你:
Is the user
assumed to be in the database already? 是否假定user
已经在数据库中?
What is the default value of isAdmin
in the database? 数据库中isAdmin
的默认值是什么?
Are you passing the isAdmin
attribute as an input from a form or something? 您是否将isAdmin
属性作为表单或其他内容的输入传递?
And why is it a store
request when you're just trying to log a user in ? 为什么它是一个store
时,你只是想登录用户请求?
It's a bit confusing. 这有点令人困惑。 I can tell from your code that you're trying to log a user in, but you're doing it in a store
method (nothing wrong with that, just convention), the store
method is usually used in storing data (how coincidental!) 我可以从您的代码中看出您正在尝试登录用户,但是您正在使用store
方法(这没什么问题,只是约定), store
方法通常用于存储数据(巧合! )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.