我正在尝试通过登录页面访问管理页面,但出现错误:
ErrorException Undefined property
Illuminate\Auth\AuthManager::$isAdmin
app \ Http \ Middleware \ Admin.php
public function handle($request, Closure $next)
{
if(auth()->isAdmin == 1){
return $next($request);
}
return redirect('login')->with('error', 'you don\'t have admin access');
}
app \ Http \ Kernel.php
'admin' => \App\Http\Middleware\Admin::class,
app \ Http \ Providers \ User.php
protected $fillable = [
'name', 'email', 'password', 'isAdmin'
];
数据库\迁移\ 2014_10_12_000000_create_users_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->date('birth')->nullable();
$table->string('image')->nullable();
$table->string('email')->unique();
$table->float('balance')->default(0);
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('isAdmin')->default(0);
$table->rememberToken();
$table->timestamps();
});
}
有人可以帮我弄清楚吗?谢谢!
答案 0 :(得分:0)
您的中间件应该看起来像这样
public function handle($request, Closure $next)
{
if($request->user()->isAdmin == 1){
return $next($request);
}
return redirect('login')->with('error', 'you don\'t have admin access');
}
这是我的一个应用程序中的实际/工作中间件:
class IsAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (($request->user()->profile->role !== 'admin')) {
return redirect('home')->with('warning', 'Access denied! Contact admin.');
}
return $next($request);
}
}
在您的情况下:if(($ request-> user()-> isAdmin == 1)){