我用來檢查用戶是否是帶有中間件的管理員,但是5.3不可能。我之前已經使用過這個代碼:Laravel 5.3中間件(Auth :: user())= null
if(Auth::guest()) //If the user is just a guest
{
return redirect("/");
}
if(Auth::user()->isAdmin == 1) //If the user is logged in and he is the Admin
{
return $next($request);
}
else //If the user is not the admin he can't see this page
{
return redirect("/");
}
}
目前我已經嘗試過下面沒有運氣。任何人都可以指出我們如何檢查用戶是管理員還是普通用戶的正確方向?
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request){
if ($request->user()->role == 'Admin'){
return "yes";
}
}
return $next($request);
}
您的用戶模型是否有isAdmin字段?同時檢查您的數據庫是否檢查您嘗試登錄的用戶的字段是否已設置爲1。 – pseudoanime
'if($ request-> user() - > role ='Admin'){'這裏是關鍵......你需要使用'==',刪除後面的問題。 – Kyslik
謝謝錯過了,因爲我不得不寫出我試過的東西。它仍然說,如果用戶未登錄,您需要指定一個默認值。我還更正了代碼,謝謝 – master