簡體   English   中英

在laravel 5.4中添加管理員角色

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM