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

<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="qwawimqqmiuu" 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 - 查詢數據透視表

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

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

                  問題描述

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

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

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

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

                  $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",}]},]

                  有可能嗎?謝謝

                  解決方案

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

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

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

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

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

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

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

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

                  這也讓您有機會添加一些數據來描述整體參與者的狀態、他們何時與該項目關聯、他們何時離開該項目,或者他們的主管 (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 - 查詢數據透視表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  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 個參數)
                  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@“localhost的訪問被拒絕)
                    <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="qwawimqqmiuu" 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>
                            主站蜘蛛池模板: 日韩精品久久一区 | 久久久久亚洲国产| 亚洲黄色片免费观看 | 婷婷久久网 | 欧美在线日韩 | 精品久久一区 | 一区二区精品在线 | 亚洲欧美激情四射 | 国产一级电影在线 | 亚洲国产精品va在线看黑人 | 中文字幕一区在线观看视频 | 亚洲aⅴ | 成人片网址 | 亚洲视频网 | 久久久久精 | 成人高潮片免费视频欧美 | 成年免费大片黄在线观看岛国 | 久久av一区 | 国产精品99久久久久久动医院 | 久久久片 | 欧美日韩一区二区电影 | 国产精品区二区三区日本 | 亚洲视频一区二区三区四区 | 一级毛片视频 | av中文字幕在线 | 国产97碰免费视频 | 久久久婷| 日日骚视频 | 欧美一级久久 | 久久国产精品免费一区二区三区 | 午夜ww| 亚洲综合在线视频 | 国产精品久久久久久久久久 | 99re在线视频观看 | 91影院在线观看 | 欧美激情a∨在线视频播放 成人免费共享视频 | 国产成人精品一区二 | 国产在线对白 | 色综合美女 | 欧美一区二区三区在线观看 | 四虎影院在线观看av |