【发布时间】:2022-11-27 13:34:24
【问题描述】:
我在 admin 用户,它担任管理员角色,但是当我添加一个条件来检查用户是否是 admin 时,它总是返回 false。我找不到问题所在。 这是我的代码:
我在我的用户模型中设置了关系,代码末尾的条件将检查用户是否为管理员:
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function role() {
return $this->belongsTo('App\Models\Role');
}
public function isAdmin() {
if($this->role->name == 'administrator')
return true;
else
return false;
}
}
【问题讨论】:
标签: php laravel database web backend