2019-12-09 09:58:52 +08:00
|
|
|
<?php
|
|
|
|
namespace catchAdmin\permissions\model;
|
|
|
|
|
2019-12-09 16:22:00 +08:00
|
|
|
trait HasRolesTrait
|
2019-12-09 09:58:52 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月08日
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function roles()
|
|
|
|
{
|
2019-12-11 21:00:14 +08:00
|
|
|
return $this->belongsToMany(Roles::class, 'user_has_roles', 'role_id', 'uid');
|
2019-12-09 09:58:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月08日
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-12-11 21:00:14 +08:00
|
|
|
public function getRoles()
|
2019-12-09 09:58:52 +08:00
|
|
|
{
|
2019-12-11 21:00:14 +08:00
|
|
|
return $this->roles()->select();
|
2019-12-09 09:58:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月08日
|
|
|
|
* @param array $roles
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-12-11 21:00:14 +08:00
|
|
|
public function attach(array $roles)
|
2019-12-09 09:58:52 +08:00
|
|
|
{
|
2019-12-12 09:13:29 +08:00
|
|
|
if (empty($roles)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:52:33 +08:00
|
|
|
sort($roles);
|
|
|
|
|
2019-12-11 21:00:14 +08:00
|
|
|
return $this->roles()->attach($roles);
|
2019-12-09 09:58:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月08日
|
|
|
|
* @param array $roles
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-12-11 21:00:14 +08:00
|
|
|
public function detach(array $roles = [])
|
2019-12-09 09:58:52 +08:00
|
|
|
{
|
2019-12-11 21:00:14 +08:00
|
|
|
if (empty($roles)) {
|
|
|
|
return $this->roles()->detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->roles()->detach($roles);
|
2019-12-09 09:58:52 +08:00
|
|
|
}
|
|
|
|
}
|