久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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 class="qwawimqqmiuu" 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 eloquent hasManyThrough / belongsToManyThrough relationships(Laravel 5 雄辯的 hasManyThrough/belongsToManyThrough 關系)

          <tbody id='9pNAe'></tbody>

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

        • <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 class="qwawimqqmiuu" 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 關系的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                在 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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                PHP PDO ODBC connection(PHP PDO ODBC 連接)
                Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)

                  <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>
                        <tbody id='PWkC6'></tbody>
                    • <small id='PWkC6'></small><noframes id='PWkC6'>

                      • <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 class="qwawimqqmiuu" id='PWkC6'><tfoot id='PWkC6'></tfoot><dl id='PWkC6'><fieldset id='PWkC6'></fieldset></dl></div>
                          主站蜘蛛池模板: 日韩不卡av| 一本久久道 | 亚洲国产欧美日韩 | 午夜999| 精品一区二区免费视频 | 欧美黄色网 | 高清一区二区 | 99精品视频免费观看 | 日韩毛片视频 | 久久爱影视i| 日韩精品一区在线观看 | 特黄一级片 | 国产一区二区三区 | 小sao货撅起屁股扒开c微博 | 色综合久久88 | 欧美成人精品欧美一级私黄 | 久久久黄色片 | 在线观看黄色小视频 | 日韩成人免费视频 | 色综合久久久久 | 欧美一级做性受免费大片免费 | 美女黄色免费网站 | 影音先锋在线视频 | 成人久久av | 黄色大毛片 | 亚洲高清中文字幕 | 天天干影院| 91日韩在线 | 午夜视频一区二区 | www.操| 综合色婷婷一区二区亚洲欧美国产 | 日本成人一区二区三区 | 日韩色网站 | 午夜你懂的 | 婷婷在线视频 | 中文字幕一区二区三区四区 | 日韩三级黄色片 | 欧美网站在线观看 | 一级黄色免费视频 | 中文字幕av片| 在线播放黄色 |