问题
I've been able to extend the Backend\Models\User
class and add a scoped query method to retrieve only super users:
public function boot()
{
User::extend(function($model) {
$model->addDynamicMethod('scopeIsSuperUser', function($query) {
return $query->where('is_superuser', 1);
});
});
}
How can I have a scope method for users who are in a specific group? Like I only want users whose role is "BookManager
". Is it possible to use the $groups
relation already defined on the Backend\Models\User
class?
public $belongsToMany = [
'groups' => ['Backend\Models\UserGroup', 'table' => 'backend_users_groups']
];
回答1:
This should do it
User::extend(function($model) {
$model->addDynamicMethod('scopeIsBookManager', function($query) {
return $query->whereHas('groups', function ($query) {
$query->where('code', 'BookManager');
});
});
});
来源:https://stackoverflow.com/questions/40797227/october-cms-how-to-extend-the-backend-user-with-role-scope