久久久久久久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 關(guān)系

      Laravel 5 eloquent hasManyThrough / belongsToManyThrough relationships(Laravel 5 雄辯的 hasManyThrough/belongsToManyThrough 關(guān)系)

          <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 關(guān)系的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                問題描述

                在 Laravel 5.2 應(yīng)用程序中,我有三個(gè)模型:UserRoleTask.一個(gè)User關(guān)聯(lián)多個(gè)Roles,一個(gè)Role關(guān)聯(lián)多個(gè)Tasks.因此,每個(gè)用戶都通過他們的角色與多個(gè)任務(wù)相關(guān)聯(lián).

                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 關(guān)聯(lián)的所有 Tasks.

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

                我的模型的相關(guān)部分如下所示:

                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錯(cuò)誤;

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

                它似乎試圖通過 Role 模型中的(不存在的)外鍵訪問關(guān)系,而不是通過數(shù)據(jù)透視表.

                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;
                

                如何通過這些關(guān)系訪問與用戶相關(guān)的所有任務(wù)?

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

                推薦答案

                我開發(fā)了一個(gè)自定義BelongsToManyThrough 關(guān)系,您可能會(huì)感興趣.您需要添加新的關(guān)系類(在我的要點(diǎn)中給出;粘貼在這里太長(zhǎng)了),并且還需要按照要點(diǎn)中的描述覆蓋您的基本 Model 類以實(shí)現(xiàn) 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.

                然后(假設(shè)您使用 Laravel 的默認(rèn)表命名方案 - 如果沒有,您也可以指定連接表),您可以將關(guān)系定義為:

                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 不僅會(huì)為您提供用戶的任務(wù)列表,還會(huì)告訴您每個(gè)用戶通過其擁有每個(gè)任務(wù)的角色.例如,如果您有:

                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."
                            }
                        ]
                    }
                ]
                

                我的自定義關(guān)系有效地完成了這項(xiàng)工作,只需要幾個(gè)查詢,而不是其他涉及 foreach 的解決方案,后者會(huì)創(chuàng)建一個(gè) 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.

                這篇關(guān)于Laravel 5 雄辯的 hasManyThrough/belongsToManyThrough 關(guān)系的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

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

                  <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>
                          主站蜘蛛池模板: 久久精品欧美一区二区三区不卡 | 中文字幕欧美一区二区 | 国产成人免费 | 欧美在线观看一区 | 日韩视频一区二区在线 | 午夜影院黄 | 亚洲精品一区二区网址 | 国产精品久久久久久久久久免费看 | 亚洲精品在线视频 | 欧美性另类 | 超碰婷婷 | 性高湖久久久久久久久aaaaa | 91亚洲国产亚洲国产 | 亚洲一区二区三区在线播放 | 国产亚洲一区二区三区在线观看 | 午夜国产精品视频 | 国产一区二区精华 | www.蜜桃av| 日韩欧美在线视频播放 | 精品亚洲第一 | av片网| 欧美视频精品 | 日韩手机视频 | 操到爽 | 欧美福利视频一区 | 丁香色婷婷 | 在线看亚洲| 久久久久久综合 | 国产精品777一区二区 | 国产黄色网址在线观看 | 国产一区精品在线 | 亚洲男人天堂网 | 伊人春色成人网 | 国产免费观看一区 | 国产视频一区二区三区四区五区 | 久久综合激情 | 欧美在线播放一区 | 国产亚洲成av人在线观看导航 | 久久久一区二区 | 亚洲在线一区二区 | 久久激情网 |