在 Laravel 中的 Session 中调用未定义的方法 App\User::admin() 错误
Call to undefined method App\User::admin() error in Session in Laravel
我正在尝试在我的项目中添加一些会话,当我注销时它工作正常。但是当我以管理员身份登录时,出现以下错误:
Call to undefined method App\User::admin()
这是我的路线
Route::resource('/create','PagesController@showCreate')->middleware(IsAdmin::class);
Route::get('/users','UserController@index')->middleware(IsAdmin::class);
Route::get('/verify','UserController@userVerify')->middleware(IsAdmin::class);
这是我的用户模型
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
* protected $table = 'users';
*public $primaryKey ='id';
* @var array
*/
protected $fillable = [
'name', 'email', 'password','userType',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
'admin' => 'boolean',
];
public function posts(){
return $this->hasMany('App\Post');
}
public function isAdmin()
{
return $this->admin;
}
}
这是我的 IsAdmin class
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class IsAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
{
if ( Auth::check() && Auth::user()->admin() )
{
return $next($request);
}
return redirect('home');
}
}
}
每次我尝试重定向到其他路线时,我都会收到相同的错误,但仪表板除外
更改 IsAdmin 中的 if 语句 class:
if ( Auth::check() && Auth::user()->isAdmin() )
我正在尝试在我的项目中添加一些会话,当我注销时它工作正常。但是当我以管理员身份登录时,出现以下错误:
Call to undefined method App\User::admin()
这是我的路线
Route::resource('/create','PagesController@showCreate')->middleware(IsAdmin::class);
Route::get('/users','UserController@index')->middleware(IsAdmin::class);
Route::get('/verify','UserController@userVerify')->middleware(IsAdmin::class);
这是我的用户模型
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
* protected $table = 'users';
*public $primaryKey ='id';
* @var array
*/
protected $fillable = [
'name', 'email', 'password','userType',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
'admin' => 'boolean',
];
public function posts(){
return $this->hasMany('App\Post');
}
public function isAdmin()
{
return $this->admin;
}
}
这是我的 IsAdmin class
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class IsAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
{
if ( Auth::check() && Auth::user()->admin() )
{
return $next($request);
}
return redirect('home');
}
}
}
每次我尝试重定向到其他路线时,我都会收到相同的错误,但仪表板除外
更改 IsAdmin 中的 if 语句 class:
if ( Auth::check() && Auth::user()->isAdmin() )