pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <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="38jao8i" 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="3twrydc" 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à)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                在 Laravel 5.2 應(yīng)用程序中,我有三個(gè)模型:User、RoleTask.一個(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)中給出;粘貼在這里太長了),并且還需要按照要點(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)容來源于互聯(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="2dnm8se" id='PWkC6'><tfoot id='PWkC6'></tfoot><dl id='PWkC6'><fieldset id='PWkC6'></fieldset></dl></div>
                          主站蜘蛛池模板: 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 森旺-A级防火板_石英纤维板_不燃抗菌板装饰板_医疗板 | 数码管_LED贴片灯_LED数码管厂家-无锡市冠卓电子科技有限公司 | 超声波电磁流量计-液位计-孔板流量计-料位计-江苏信仪自动化仪表有限公司 | 【孔氏陶粒】建筑回填陶粒-南京/合肥/武汉/郑州/重庆/成都/杭州陶粒厂家 | 软文发布平台 - 云软媒网络软文直编发布营销推广平台 | 防水套管|柔性防水套管|伸缩器|伸缩接头|传力接头-河南伟创管道 防水套管_柔性防水套管_刚性防水套管-巩义市润达管道设备制造有限公司 | 海鲜池-专注海鲜鱼缸、移动海鲜缸、饭店鱼缸设计定做-日晟水族厂家 | 扬尘在线监测系统_工地噪声扬尘检测仪_扬尘监测系统_贝塔射线扬尘监测设备「风途物联网科技」 | 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 对辊式破碎机-对辊制砂机-双辊-双齿辊破碎机-巩义市裕顺机械制造有限公司 | 蜘蛛车-登高车-高空作业平台-高空作业车-曲臂剪叉式升降机租赁-重庆海克斯公司 | 定硫仪,量热仪,工业分析仪,马弗炉,煤炭化验设备厂家,煤质化验仪器,焦炭化验设备鹤壁大德煤质工业分析仪,氟氯测定仪 | 求是网 - 思想建党 理论强党 | 中视电广_短视频拍摄_短视频推广_短视频代运营_宣传片拍摄_影视广告制作_中视电广 | 玻纤土工格栅_钢塑格栅_PP焊接_单双向塑料土工格栅_复合防裂布厂家_山东大庚工程材料科技有限公司 | 武汉高温老化房,恒温恒湿试验箱,冷热冲击试验箱-武汉安德信检测设备有限公司 | 照相馆预约系统,微信公众号摄影门店系统,影楼管理软件-盟百网络 | 青州开防盗门锁-配汽车芯片钥匙-保险箱钥匙-吉祥修锁店 | 成都办公室装修-办公室设计-写字楼装修设计-厂房装修-四川和信建筑装饰工程有限公司 | 北京公积金代办/租房发票/租房备案-北京金鼎源公积金提取服务中心 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 冲锋衣滑雪服厂家-冲锋衣定制工厂-滑雪服加工厂-广东睿牛户外(S-GERT) | 闸阀_截止阀_止回阀「生产厂家」-上海卡比阀门有限公司 | 丹佛斯变频器-Danfoss战略代理经销商-上海津信变频器有限公司 | 福建珂朗雅装饰材料有限公司「官方网站」 | 成都办公室装修-办公室设计-写字楼装修设计-厂房装修-四川和信建筑装饰工程有限公司 | 钢骨架轻型板_膨石轻型板_钢骨架轻型板价格_恒道新材料 | 实验室隔膜泵-无油防腐蚀隔膜泵-耐腐蚀隔膜真空泵-杭州景程仪器 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 锥形螺带干燥机(新型耙式干燥机)百科-常州丰能干燥工程 | 昆山PCB加工_SMT贴片_PCB抄板_线路板焊接加工-昆山腾宸电子科技有限公司 | 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | 环境模拟实验室_液体-气体控温机_气体控温箱_无锡双润冷却科技有限公司 | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | 高铝轻质保温砖_刚玉莫来石砖厂家_轻质耐火砖价格 | 有福网(yofus.com)洗照片冲印,毕业聚会纪念册相册制作个性DIY平台 | 地埋式垃圾站厂家【佳星环保】小区压缩垃圾中转站转运站 | 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 信阳市建筑勘察设计研究院有限公司 | 鄂泉泵业官网|(杭州、上海、全国畅销)大流量防汛排涝泵-LW立式排污泵 |