October CMS: How to extend the backend user with role scope

拜拜、爱过 提交于 2019-12-10 18:23:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!