recover
This commit is contained in:
58
catch/permissions/model/HasRolesTrait.php
Normal file
58
catch/permissions/model/HasRolesTrait.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions\model;
|
||||
|
||||
trait HasRolesTrait
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @return mixed
|
||||
*/
|
||||
public function roles()
|
||||
{
|
||||
return $this->belongsToMany(Roles::class, 'user_has_roles', 'role_id', 'uid');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @param array $fields
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRoles()
|
||||
{
|
||||
return $this->roles()->select();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @param array $roles
|
||||
* @return mixed
|
||||
*/
|
||||
public function attach(array $roles)
|
||||
{
|
||||
if (empty($roles)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
sort($roles);
|
||||
|
||||
return $this->roles()->attach($roles);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @param array $roles
|
||||
* @return mixed
|
||||
*/
|
||||
public function detach(array $roles = [])
|
||||
{
|
||||
if (empty($roles)) {
|
||||
return $this->roles()->detach();
|
||||
}
|
||||
|
||||
return $this->roles()->detach($roles);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user