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

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

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

      <bdo id='IeUDp'></bdo><ul id='IeUDp'></ul>

  • <legend id='IeUDp'><style id='IeUDp'><dir id='IeUDp'><q id='IeUDp'></q></dir></style></legend>
    <tfoot id='IeUDp'></tfoot>

      1. Laravel Eloquent - 查詢數(shù)據(jù)透視表

        Laravel Eloquent - querying pivot table(Laravel Eloquent - 查詢數(shù)據(jù)透視表)

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

          <tbody id='sFBJ5'></tbody>
        <legend id='sFBJ5'><style id='sFBJ5'><dir id='sFBJ5'><q id='sFBJ5'></q></dir></style></legend>
        • <bdo id='sFBJ5'></bdo><ul id='sFBJ5'></ul>

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

                  <tfoot id='sFBJ5'></tfoot>

                • 本文介紹了Laravel Eloquent - 查詢數(shù)據(jù)透視表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  在我的 Laravel 應用程序中,我有三個數(shù)據(jù)庫表,分別稱為用戶、項目和角色.它們之間存在 m:n 關系,所以我還有名為 project_user_role 的數(shù)據(jù)透視表.數(shù)據(jù)透視表包含 user_id、project_id 和 role_id 列.請參閱 MySQL Workbench 的屏幕截圖圖像.

                  我的 User、Project 和 Role 模型被定義為屬于這樣的關系:

                  //用戶模型示例公共功能項目(){return $this->belongsToMany('AppLibraryModelsProject', 'project_user_role')->withPivot(['user_id','role_id']);}

                  現(xiàn)在我可以輕松地獲得這樣的認證用戶的項目:

                  $user = Auth::user();$projects = $user->projects;

                  這看起來像這樣:

                  <預><代碼>[{身份證":1,"name": "測試項目",用戶 ID":1,"created_at": "2018-05-01 01:02:03","updated_at": "2018-05-01 01:02:03",樞軸":{用戶 ID":2,project_id":17,角色 ID":1}},]

                  但我想將有關用戶角色的信息注入"到響應中:

                  <預><代碼>[{身份證":1,"name": "測試項目",用戶 ID":1,"created_at": "2018-05-01 01:02:03","updated_at": "2018-05-01 01:02:03",樞軸":{用戶 ID":2,project_id":17,角色 ID":1},角色: [{身份證":1,"name": "一些角色名稱","display_name": "一些角色名稱","description": "一些角色名稱","created_at": "2018-05-01 01:02:03","updated_at": "2018-05-01 01:02:03",}]},]

                  有可能嗎?謝謝

                  解決方案

                  您實際上是在要求對數(shù)據(jù)透視表進行預加載.問題是,數(shù)據(jù)透視表中的數(shù)據(jù)不是來自頂級模型類,因此沒有任何關系方法供您參考.

                  您的數(shù)據(jù)庫結構也有一點尷尬,因為您的數(shù)據(jù)透視表連接了三個表而不是兩個.不過,在實際回答您的問題后,我會對此進行一些思考...

                  因此,您可以通過數(shù)據(jù)透視表從用戶轉(zhuǎn)到項目.您可以通過數(shù)據(jù)透視表從用戶轉(zhuǎn)到角色.但是您要尋找的是通過該數(shù)據(jù)透視表從項目轉(zhuǎn)到角色.(即您想要的數(shù)據(jù)報顯示項目數(shù)據(jù)是具有嵌套角色"的頂級.).只有當項目模型是您的入口點而不是您的用戶時,才能這樣做.

                  因此,首先向名為 roles 的項目模型添加多對多關系方法,然后像這樣運行查詢:

                  app(Projects::class)->with('roles')->wherePivot('user_id', Auth::user()->getKey())->get()

                  至于結構,我認為您在那里違反了單一職責.用戶"代表個人.但是您也使用它來表示項目的參與者"的概念.我相信您需要一個新的 Participant 表,它與 User 是多對一的關系,與 Project 是一對一的關系.那么您的數(shù)據(jù)透視表只需要在 Participant 和 Role 之間進行多對多,而將 User 排除在外.

                  那么您的查詢將如下所示:

                  Auth::user()->participants()->with(['project', 'roles'])->get()

                  這也讓您有機會添加一些數(shù)據(jù)來描述整體參與者的狀態(tài)、他們何時與該項目關聯(lián)、他們何時離開該項目,或者他們的主管 (parent_participant_id) 可能是誰.

                  in my Laravel app I have three database tables called users, projects and roles. There is m:n relation between them so I have also pivot table called project_user_role. Pivot table contains user_id, project_id and role_id columns. See image for screenshot from MySQL Workbench.

                  My User, Project and Role models got defined belongsToMany relation like that:

                  //User model example
                  public function projects()
                  {
                      return $this->belongsToMany('AppLibraryModelsProject', 'project_user_role')->withPivot(['user_id','role_id']);
                  }
                  

                  Now I can easily get projects of authenticated user like that:

                  $user = Auth::user();
                  $projects = $user->projects;
                  

                  Response of this looks like that:

                  [
                    {
                        "id": 1,
                        "name": "Test project",
                        "user_id": 1,
                        "created_at": "2018-05-01 01:02:03",
                        "updated_at": "2018-05-01 01:02:03",
                        "pivot": {
                            "user_id": 2,
                            "project_id": 17,
                            "role_id": 1
                        }
                    },
                  ]
                  

                  but I would like to "inject" information about user role into response likat that:

                  [
                    {
                        "id": 1,
                        "name": "Test project",
                        "user_id": 1,
                        "created_at": "2018-05-01 01:02:03",
                        "updated_at": "2018-05-01 01:02:03",
                        "pivot": {
                            "user_id": 2,
                            "project_id": 17,
                            "role_id": 1
                        },
                        roles: [
                          {
                              "id": 1,
                              "name": "some role name",
                              "display_name": "Some role name",
                              "description": "Some role name",
                              "created_at": "2018-05-01 01:02:03",
                              "updated_at": "2018-05-01 01:02:03",
                          }
                        ]
                    },
                  ]
                  

                  Is it possible? Thanks

                  解決方案

                  You're essentially asking for an eager-load on a pivot table. The problem is, the data from the pivot table isn't coming from a top-level Model class, so there isn't anything in the way of a relationship method for you to reference.

                  There's a little awkwardness in your DB structure too, in that your pivot table is joining three tables instead of two. I'll get into some thoughts on that after actually answering your question though...

                  So, you can go from the User to the Project through the pivot table. And you can go from the User to the Role through your pivot table. But what you're looking for is to go from the Project to the Role through that pivot table. (i.e. your desired datagram shows the project data to be top-level with nested 'roles'.) . This can only be done if the Projects model is your entry point as opposed to your User.

                  So start by adding a many-to-many relation method to your Projects Model called roles, and run your query like this:

                  app(Projects::class)->with('roles')->wherePivot('user_id', Auth::user()->getKey())->get()
                  

                  As for the structure, I think you have a little bit of a single-responsibility violation there. "User" represents an individual person. But you're also using it to represent the concept of a "Participant" of a project. I believe you need a new Participant table that has a many-to-one relationship with User, and a one-to-one relationship with Project. Then your pivot table need only be a many-to-many between Participant and Role, and leave User out of it.

                  Then your query would look like this:

                  Auth::user()->participants()->with(['project', 'roles'])->get()
                  

                  This would also give you the opportunity to add some data describing things like what the overall participant.status is, when they became associated with that project, when they left that project, or who their supervisor (parent_participant_id) might be.

                  這篇關于Laravel Eloquent - 查詢數(shù)據(jù)透視表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數(shù))
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問被拒絕)
                    <bdo id='tZbVD'></bdo><ul id='tZbVD'></ul>
                    <tfoot id='tZbVD'></tfoot>
                      <tbody id='tZbVD'></tbody>
                  • <small id='tZbVD'></small><noframes id='tZbVD'>

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

                          1. <legend id='tZbVD'><style id='tZbVD'><dir id='tZbVD'><q id='tZbVD'></q></dir></style></legend>
                            主站蜘蛛池模板: 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 澳威全屋定制官网|极简衣柜十大品牌|衣柜加盟代理|全屋定制招商 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 红酒招商加盟-葡萄酒加盟-进口红酒代理-青岛枞木酒业有限公司 | 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 | ORP控制器_ORP电极价格-上优泰百科 | 电子书导航网_电子书之家_电子书大全_最新电子书分享发布平台 | 液压油缸-液压缸厂家价格,液压站系统-山东国立液压制造有限公司 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 中原网视台| 防水套管厂家_刚性防水套管_柔性防水套管_不锈钢防水套管-郑州中泰管道 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 美甲贴片-指甲贴片-穿戴美甲-假指甲厂家--薇丝黛拉 | 穿线管|波纹穿线管|包塑金属软管|蛇皮管?闵彬专注弱电工程? | 沈阳液压泵_沈阳液压阀_沈阳液压站-沈阳海德太科液压设备有限公司 | 防爆电机-高压防爆电机-ybx4电动机厂家-河南省南洋防爆电机有限公司 | GAST/BRIWATEC/CINCINNATI/KARL-KLEIN/ZIEHL-ABEGG风机|亚喜科技 | 潍坊大集网-潍坊信息港-潍坊信息网 | 骨密度仪-骨密度测定仪-超声骨密度仪-骨龄测定仪-天津开发区圣鸿医疗器械有限公司 | 工控机-图像采集卡-PoE网卡-人工智能-工业主板-深圳朗锐智科 | 沥青车辙成型机-车托式混凝土取芯机-混凝土塑料试模|鑫高仪器 | 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 盐城网络公司_盐城网站优化_盐城网站建设_盐城市启晨网络科技有限公司 | 冷藏车厂家|冷藏车价格|小型冷藏车|散装饲料车厂家|程力专用汽车股份有限公司销售十二分公司 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 丝杆升降机-不锈钢丝杆升降机-非标定制丝杆升降机厂家-山东鑫光减速机有限公司 | 基本型顶空进样器-全自动热脱附解吸仪价格-AutoHS全模式-成都科林分析技术有限公司 | 学校用栓剂模,玻璃瓶轧盖钳,小型安瓿熔封机,实验室安瓿熔封机-长沙中亚制药设备有限公司 | HV全空气系统_杭州暖通公司—杭州斯培尔冷暖设备有限公司 | 超高频感应加热设备_高频感应电源厂家_CCD视觉检测设备_振动盘视觉检测设备_深圳雨滴科技-深圳市雨滴科技有限公司 | 建筑消防设施检测系统检测箱-电梯**检测仪器箱-北京宇成伟业科技有限责任公司 | 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 间苯二酚,间苯二酚厂家-淄博双和化工 | 山东臭氧发生器,臭氧发生器厂家-山东瑞华环保设备 | 二手电脑回收_二手打印机回收_二手复印机回_硒鼓墨盒回收-广州益美二手电脑回收公司 | 求是网 - 思想建党 理论强党 | 短信营销平台_短信群发平台_106短信发送平台-河南路尚 | 工业雾炮机_超细雾炮_远程抑尘射雾器-世纪润德环保设备 | 山东商品混凝土搅拌楼-环保型搅拌站-拌合站-分体仓-搅拌机厂家-天宇 | 高压包-点火器-高压发生器-点火变压器-江苏天网 |