<legend id='0c0cO'><style id='0c0cO'><dir id='0c0cO'><q id='0c0cO'></q></dir></style></legend>

  1. <tfoot id='0c0cO'></tfoot>
    <i id='0c0cO'><tr id='0c0cO'><dt id='0c0cO'><q id='0c0cO'><span id='0c0cO'><b id='0c0cO'><form id='0c0cO'><ins id='0c0cO'></ins><ul id='0c0cO'></ul><sub id='0c0cO'></sub></form><legend id='0c0cO'></legend><bdo id='0c0cO'><pre id='0c0cO'><center id='0c0cO'></center></pre></bdo></b><th id='0c0cO'></th></span></q></dt></tr></i><div id='0c0cO'><tfoot id='0c0cO'></tfoot><dl id='0c0cO'><fieldset id='0c0cO'></fieldset></dl></div>

    1. <small id='0c0cO'></small><noframes id='0c0cO'>

        <bdo id='0c0cO'></bdo><ul id='0c0cO'></ul>
    2. Laravel eloquent按关系模型上的角色名称排序

      时间:2023-09-22

        <legend id='hgqOy'><style id='hgqOy'><dir id='hgqOy'><q id='hgqOy'></q></dir></style></legend>

      1. <small id='hgqOy'></small><noframes id='hgqOy'>

        • <tfoot id='hgqOy'></tfoot>

            <tbody id='hgqOy'></tbody>
          <i id='hgqOy'><tr id='hgqOy'><dt id='hgqOy'><q id='hgqOy'><span id='hgqOy'><b id='hgqOy'><form id='hgqOy'><ins id='hgqOy'></ins><ul id='hgqOy'></ul><sub id='hgqOy'></sub></form><legend id='hgqOy'></legend><bdo id='hgqOy'><pre id='hgqOy'><center id='hgqOy'></center></pre></bdo></b><th id='hgqOy'></th></span></q></dt></tr></i><div id='hgqOy'><tfoot id='hgqOy'></tfoot><dl id='hgqOy'><fieldset id='hgqOy'></fieldset></dl></div>

                <bdo id='hgqOy'></bdo><ul id='hgqOy'></ul>
              • 本文介绍了Laravel eloquent按关系模型上的角色名称排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我遇到了一个问题,我必须根据模型的关系数据对模型集合进行排序/排序.

                I'm stuck with a problem where I have to sort / order a collection of models by their relationship's data.

                我的设置如下:

                型号:UserTeamTeamUserRole

                TeamUser 模型是一个数据透视模型/表(包含 user_idteam_id.如果值得一提,我也使用 spatie/laravel-permissions角色.

                The TeamUser model is a pivot model / table (containing user_id and team_id. If it's worth mentioning I am also using spatie/laravel-permissions for the roles.

                如果我想按role.name 对团队中的用户进行排序,我该怎么做?我正在谈论 Team 模型中的 users() 关系(请参阅代码示例的进一步内容).一些用户具有 team-leader 角色,大多数用户具有 team-seller 角色.我试过做一个普通的 ..->sortBy('role.name') 但这似乎不起作用.如果有人可以帮助我,请提前致谢.

                How would I go forth when I want to sort the users in a team by their role.name? I'm talking about the users() relation in the Team model (see further down for code sample). Some users have the role team-leader and most have the role team-seller. I've tried doing a ordinary ..->sortBy('role.name') but that doesn't seem to work. Thanks in advance if anyone could help me out.

                User.php

                /**
                 * Team relation
                 *
                 * @return IlluminateDatabaseEloquentRelationsBelongsToMany
                 */
                public function team()
                {
                    return $this->belongsToMany('AppTeam', 'team_users', 'user_id', 'team_id');
                }
                

                Team.php

                /**
                 * User relation
                 *
                 * @return IlluminateDatabaseEloquentRelationsBelongsToMany
                 */
                public function users()
                {
                    return $this->belongsToMany('AppUser', 'team_users', 'team_id', 'user_id')->withTimestamps();
                }
                

                推荐答案

                如果要根据嵌套关系列对结果进行排序,则必须使用连接链:

                if you want to order the result based on nested relation column, you must use a chain of joins:

                $values = Team::query()
                      ->leftJoin('users', 'users.team_id', '=', 'teams.id')
                      ->leftJoin('model_has_roles', function ($join) {
                          $join->on('model_has_roles.model_id', '=', 'users.id')
                               ->where('model_has_roles.model_type', '=', 'appModelsUser');
                      })
                      ->leftJoin('roles', 'roles.id', '=', 'model_has_roles.role_id')
                      ->orderBy('roles.name')
                      ->get();
                

                我试过了,效果很好.

                请注意,如果您想按多列排序,您可以根据需要添加 'orderBy' 子句:

                please note that if you want to order by multiple columns you could add 'orderBy' clause as much as you want:

                ->orderBy('roles.name', 'DESC')->orderby('teams.name', 'ASC') //... ext
                

                这篇关于Laravel eloquent按关系模型上的角色名称排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel 5 雄辩的 whereIn 下一篇:基于多个 ID 检索 Laravel 模型结果

                相关文章

                最新文章

                <small id='BNVIu'></small><noframes id='BNVIu'>

              • <tfoot id='BNVIu'></tfoot>

                <legend id='BNVIu'><style id='BNVIu'><dir id='BNVIu'><q id='BNVIu'></q></dir></style></legend>
                    <bdo id='BNVIu'></bdo><ul id='BNVIu'></ul>
                  1. <i id='BNVIu'><tr id='BNVIu'><dt id='BNVIu'><q id='BNVIu'><span id='BNVIu'><b id='BNVIu'><form id='BNVIu'><ins id='BNVIu'></ins><ul id='BNVIu'></ul><sub id='BNVIu'></sub></form><legend id='BNVIu'></legend><bdo id='BNVIu'><pre id='BNVIu'><center id='BNVIu'></center></pre></bdo></b><th id='BNVIu'></th></span></q></dt></tr></i><div id='BNVIu'><tfoot id='BNVIu'></tfoot><dl id='BNVIu'><fieldset id='BNVIu'></fieldset></dl></div>