【问题标题】:Laravel - order by number of entriesLaravel - 按条目数排序
【发布时间】:2016-09-29 10:12:03
【问题描述】:

这是question 的后续行动。我有一个看起来像这样的查询:

$users = User::leftjoin('role_user', 'users.id', '=', 'role_user.user_id')
              ->select('users.*')
              ->orderBy(DB::raw('role_id IS NULL'))
              ->groupBy('users.id')
              ->orderBy('role_id', 'asc')
              ->get();

当我这样做时,我会为用户获取不同的值,并且它们按 role_id 以升序排列,这个查询的唯一问题是拥有两个或多个角色的用户在只有一个角色的用户之后角色。例如,用户具有id=1id=2 的角色,而其他用户只有id=1 的角色,该用户将在列表中具有两个角色的用户之前。我想改变这一点,让拥有更多角色的用户高于只有一个角色的用户。

【问题讨论】:

  • 你可以使用 group_by 来避免重复
  • 你能在“角色”上尝试分组吗
  • 可以显示表格的结构吗
  • @ImtiazPabel 我已经根据您的建议更新了问题,但这仍然不是我需要的
  • @YagnikDetroja group_by 角色只会按角色对它们进行分组,这不是我所需要的,而且表结构很简单,我有一些关于他们的信息字段的用户和一个我有 id 的数据透视表, role_id 和 user_id

标签: php mysql laravel eloquent


【解决方案1】:

以下查询最终对我有用:

User::leftjoin('role_user', 'users.id', '=', 'role_user.user_id')
                               ->select('users.*')
                               ->selectRaw('user_id, count(*) as count')
                               ->orderBy(DB::raw('role_id IS NULL'))
                               ->groupBy('users.id')
                               ->orderBy('count', 'DESC')
                               ->orderBy('role_id', 'asc')
                               ->get();

【讨论】:

    【解决方案2】:

    我建议使用Query ScopesQuerying Relations

    class User extends Authenticatable
    {
        ...
    
        public function roles()
        {
            return $this->belongsToMany('Role', 'role_user');
        }
    
        // Scope users who have a specified role
        public function scopeHasRole($query, $role) {
            return $query->whereHas('roles', function ($q) use ($role) {
                $q->where('id', $role);
            });
        }
    
        // Scope users who have a set of roles
        public function scopeHasRoles($query, $roles) {
            if (! is_array($roles)) {
                return $this->scopeHasRole($query, $roles);
            }
    
            return $query->whereHas('roles', function ($q) use ($roles) {
                $q->whereIn('id', $roles);
            });
        }
    
        // Scope a user who don't have any roles.
        public function scopeDoesntHaveAnyRoles($query) {
            return $query->whereDoesntHave('roles');
        }
    
    }
    

    示例用法:

    User::doesntHaveAnyRoles(); // return users don't have any roles.
    
    User::hasRole(1)->orderBy('id', 'asc')->get(); // return users who belongs to role id 1.
    
    User::hasRoles([1, 2, 3]); // return users who have a set of roles.
    

    【讨论】:

    • 我正在使用这个包来获取角色和权限github.com/DynamicCodeNinja/RBAC我已经按照你的建议向他们的特征添加了方法,但是我只得到了返回的查询生成器,因为我需要所有的用户,甚至那些没有角色的人,我认为这些方法不适用于那种查询
    • 我猜你不使用get()User::doesntHaveAnyRoles()->get()User::doesntHaveAnyRoles()->paginate() ?
    • 然后我得到'完整性约束违规:1052 列'id' in where 子句不明确(SQL:select * from users where exists (select * from roles inner join role_userroles.id = role_user.role_id 其中role_user.user_id = users.idid in (1, 2)))' 当我使用用户: :hasRoles([1, 2, 3])->get();
    • 那么也许该包在定义关系时存在问题,因为目前我正在开发一个使用上面代码而没有出现该错误的项目。
    • 用户::doesntHaveAnyRoles();似乎是唯一有效的方法
    猜你喜欢
    • 2020-12-15
    • 1970-01-01
    • 2015-05-20
    • 2021-09-03
    • 2016-02-23
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多