1. <small id='9pNAe'></small><noframes id='9pNAe'>

  2. <tfoot id='9pNAe'></tfoot>
      <bdo id='9pNAe'></bdo><ul id='9pNAe'></ul>

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

      Laravel 5 雄辩的 hasManyThrough/belongsToManyThrough 关系

      时间:2023-09-24

              <tbody id='MU3PV'></tbody>

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

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

                <legend id='MU3PV'><style id='MU3PV'><dir id='MU3PV'><q id='MU3PV'></q></dir></style></legend>
                本文介绍了Laravel 5 雄辩的 hasManyThrough/belongsToManyThrough 关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在 Laravel 5.2 应用程序中,我有三个模型:UserRoleTask.一个User关联多个Roles,一个Role关联多个Tasks.因此,每个用户都通过他们的角色与多个任务相关联.

                In a Laravel 5.2 application I have three models: User, Role and Task. A User is associated with multiple Roles, and a Role is associated with multiple Tasks. Therefore each user is associated to multiple tasks, through their roles.

                我正在尝试通过他们的角色访问与 User 关联的所有 Tasks.

                I am trying to access all Tasks associated with a User, through their Roles.

                我的模型的相关部分如下所示:

                The relevant parts of my models look like:

                class User extends Authenticatable
                {    
                    public function roles()
                    {
                        return $this->belongsToMany('AppRole');
                    }
                
                    public function tasks()
                    {
                        return $this->hasManyThrough('AppTask', 'AppRole');
                    }
                }
                
                class Role extends Model
                {
                    public function tasks()
                    {
                        return $this->belongsToMany('AppTask');
                    }
                
                    public function users()
                    {
                        return $this->belongsToMany('AppUser');
                    }
                }
                
                class Task extends Model
                {    
                    public function roles()
                    {
                        return $this->belongsToMany('AppRole');
                    } 
                }
                

                以下返回SQL错误;

                Column not found: 1054 Unknown column 'roles.user_id'
                

                它似乎试图通过 Role 模型中的(不存在的)外键访问关系,而不是通过数据透视表.

                It seems to be trying to access the relationship through a (non-existent) foreign key in the Role model, rather than through the pivot table.

                $user = Auth::user;
                $tasks = $user->tasks;
                

                如何通过这些关系访问与用户相关的所有任务?

                How can I access all tasks related to a user through these relationships?

                推荐答案

                我开发了一个自定义BelongsToManyThrough 关系,您可能会感兴趣.您需要添加新的关系类(在我的要点中给出;粘贴在这里太长了),并且还需要按照要点中的描述覆盖您的基本 Model 类以实现 belongsToManyThrough.

                I have developed a custom BelongsToManyThrough relationship which might interest you. You would need to add the new relation class (as given in my gist; it is too long to paste here), and also override your base Model class as described in the gist to implement belongsToManyThrough.

                然后(假设您使用 Laravel 的默认表命名方案 - 如果没有,您也可以指定连接表),您可以将关系定义为:

                Then (assuming you are using Laravel's default table naming schemes - if not, you can specify the joining tables as well), you would define your relationship as:

                public function tasks()
                {
                    return $this->belongsToManyThrough(
                        'AppTask',
                        'AppRole');
                }
                

                belongsToManyThrough 不仅会为您提供用户的任务列表,还会告诉您每个用户通过其拥有每个任务的角色.例如,如果您有:

                belongsToManyThrough will not only give you a list of Tasks for your User(s), it will also tell you the Role(s) via which each User has each Task. For example, if you had:

                $user->tasks()->get()

                输出将类似于:

                 [
                    {
                        "id": 2,
                        "name": "Ban spammers",
                        "roles_via": [
                            {
                                "id": 2,
                                "slug": "site-admin",
                                "name": "Site Administrator",
                                "description": "This role is meant for "site administrators", who can basically do anything except create, edit, or delete other administrators."
                            },
                            {
                                "id": 3,
                                "slug": "group-admin",
                                "name": "Group Administrator",
                                "description": "This role is meant for "group administrators", who can basically do anything with users in their same group, except other administrators of that group."
                            }
                        ]
                    },
                    {
                        "id": 13,
                        "name": "Approve posts",
                        "roles_via": [
                            {
                                "id": 3,
                                "slug": "group-admin",
                                "name": "Group Administrator",
                                "description": "This role is meant for "group administrators", who can basically do anything with users in their same group, except other administrators of that group."
                            }
                        ]
                    },
                    {
                        "id": 16,
                        "name": "Reboot server",
                        "roles_via": [
                            {
                                "id": 2,
                                "slug": "site-admin",
                                "name": "Site Administrator",
                                "description": "This role is meant for "site administrators", who can basically do anything except create, edit, or delete other administrators."
                            }
                        ]
                    }
                ]
                

                我的自定义关系有效地完成了这项工作,只需要几个查询,而不是其他涉及 foreach 的解决方案,后者会创建一个 n+1 查询 问题.

                My custom relationship does this efficiently, with only a few queries, as opposed to other solutions involving foreach, which would create an n+1 query problem.

                这篇关于Laravel 5 雄辩的 hasManyThrough/belongsToManyThrough 关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel 不允许我迁移表,因为它已经存在 下一篇:PHP 将 Excel 导入数据库 (xls &amp; xlsx)

                相关文章

                最新文章

                1. <legend id='PWkC6'><style id='PWkC6'><dir id='PWkC6'><q id='PWkC6'></q></dir></style></legend>

                    <tfoot id='PWkC6'></tfoot>
                      <bdo id='PWkC6'></bdo><ul id='PWkC6'></ul>
                  1. <small id='PWkC6'></small><noframes id='PWkC6'>

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