[英]Add Admin role in laravel 5.4
我有一個數據庫,其中:Admin具有True isAdmin屬性,但其他用戶具有false isAdmin屬性。
我想檢查誰登錄的用戶是管理員或不將其重定向到我的應用程序不同的頁面。 我在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
方法通常用於存儲數據(巧合! )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.