我想检查用户是否是管理员。User table
id | role_id | name
1 | 3 | test
Role table
id | role
1 | user
2 | employee
3 | admin
User model
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'gender', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function roles()
{
return $this->belongsToMany('App\Role');
}
public function isAdmin() {
return $this->roles()->where('role', 'user')->exists();
}
}
Role model
class Role extends Model
{
//
}
Blade template
@if(Auth::user()->isAdmin())
user is admin
@endif
我找不到答案,我必须在
function isAdmin
中添加什么都不起作用。现在我收到错误基表或未找到 View 。 最佳答案
试试这个
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'gender', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->belongsTo('App\Role');
}
public function isAdmin() {
if($this->role->name == 'admin'){
return true;
}
return false;
}
关于php - 检查用户是否是 Laravel 中的管理员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39569327/