<legend id='TFOsk'><style id='TFOsk'><dir id='TFOsk'><q id='TFOsk'></q></dir></style></legend>
      <bdo id='TFOsk'></bdo><ul id='TFOsk'></ul>
  • <small id='TFOsk'></small><noframes id='TFOsk'>

    1. <tfoot id='TFOsk'></tfoot>
        <i id='TFOsk'><tr id='TFOsk'><dt id='TFOsk'><q id='TFOsk'><span id='TFOsk'><b id='TFOsk'><form id='TFOsk'><ins id='TFOsk'></ins><ul id='TFOsk'></ul><sub id='TFOsk'></sub></form><legend id='TFOsk'></legend><bdo id='TFOsk'><pre id='TFOsk'><center id='TFOsk'></center></pre></bdo></b><th id='TFOsk'></th></span></q></dt></tr></i><div id='TFOsk'><tfoot id='TFOsk'></tfoot><dl id='TFOsk'><fieldset id='TFOsk'></fieldset></dl></div>
      1. Laravel 多对多 - 意外的结果集 ->select()

        时间:2023-09-22
          <bdo id='BFkwE'></bdo><ul id='BFkwE'></ul>

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

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

            1. <legend id='BFkwE'><style id='BFkwE'><dir id='BFkwE'><q id='BFkwE'></q></dir></style></legend>

                • <tfoot id='BFkwE'></tfoot>

                • 本文介绍了Laravel 多对多 - 意外的结果集 ->select()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想知道是否有人可以提供帮助,因为我遇到了困难并且仍在学习 Laravel ORM.任何人都可以解释为什么,当我运行时:

                  I wonder if anyone can help, as I've hit a wall and still learning Laravel ORM. Can anyone explain why, when I run:

                  public function locationTags(){
                      return $this->hasMany('AppUserHasLocationTags', 'user_id')
                          ->join('location_tags AS lt', 'lt.id', '=', 'location_tag_id');
                  }
                  

                  我得到了这个结果集:(剪断了...)

                  I get this result set: (snipped...)

                  {
                      "id": 1,
                      "created_at": "2015-05-13 13:04:56",
                      "updated_at": "2015-05-13 13:04:56",
                      "email": "REMOVED",
                      "firstname": "REMOVED",
                      "lastname": "REMOVED",
                      "location_id": 0,
                      "deleted_at": null,
                      "permissions": [],
                      "location_tags": [
                          {
                              "user_id": 1,
                              "location_tag_id": 1,
                              "id": 1,
                              "created_at": "2015-05-13 13:06:28",
                              "updated_at": "2015-05-13 13:06:28",
                              "name": "Test Tag 0",
                              "location_id": 1,
                              "deleted_at": null
                          },
                          {
                              "user_id": 1,
                              "location_tag_id": 2,
                              "id": 2,
                              "created_at": "2015-05-13 11:40:21",
                              "updated_at": "2015-05-13 12:56:13",
                              "name": "Test Tag 123",
                              "location_id": 1,
                              "deleted_at": null
                          }
                      ]
                  }
                  

                  这是王牌!但是,当我开始从 location_tags 连接中选择我想要的列时,使用:

                  Which is ace! However, when I start to select the columns I want from the location_tags join, with:

                  public function locationTags(){
                      return $this->hasMany('AppUserHasLocationTags', 'user_id')
                          ->join('location_tags AS lt', 'lt.id', '=', 'location_tag_id')
                          ->select('lt.id', 'lt.name');
                  }
                  

                  我最终得到:

                  {
                      "id": 1,
                      "created_at": "2015-05-13 13:04:56",
                      "updated_at": "2015-05-13 13:04:56",
                      "email": "REMOVED",
                      "firstname": "REMOVED",
                      "lastname": "REMOVED",
                      "location_id": 0,
                      "deleted_at": null,
                      "permissions": [],
                      "location_tags": []
                  }
                  

                  谁能解释一下这是怎么回事?并可能指出我正确的方向来限制选择?谢谢!

                  Can someone explain what's going on? And possibly point me in the right direction to limit the selects? Thanks!

                  更新我也试过:

                          $query = AppUser::with(['permissions', 'locationTags' => function($query){
                              $query->select('lt.id', 'lt.name');
                          }]);
                  

                  返回相同的结果:(

                  推荐答案

                  想通了.这里的关键是你必须包含至少一个 Laravel 可以用来映射结果集的键的 select() 值.就我而言,它是 user_id,如下所示:

                  Figured it out. The key here was that you must include a select() value of at least one key that Laravel can use to map the result set. In my case it was user_id, like so:

                  public function locationTags(){
                      return $this->hasMany('AppUserHasLocationTags', 'user_id')
                          ->join('location_tags AS lt', 'lt.id', '=', 'location_tag_id')
                          ->select('user_id', 'lt.name', 'location_tag_id');
                  }
                  

                  然后返回一个更好的结果集:

                  Which then returns a much nicer results set:

                  {
                      "id": 1,
                      "created_at": "2015-05-13 13:04:56",
                      "updated_at": "2015-05-13 13:04:56",
                      "email": "REMOVED",
                      "firstname": "REMOVED",
                      "lastname": "REMOVED",
                      "location_id": 0,
                      "deleted_at": null,
                      "permissions": [],
                      "location_tags": [
                          {
                              "user_id": 1,
                              "name": "Test Tag 0",
                              "location_tag_id": 1
                          },
                          {
                              "user_id": 1,
                              "name": "Test Tag 123",
                              "location_tag_id": 2
                          }
                      ]
                  }
                  

                  希望这对未来的人有所帮助,因为它让我猜了好几个小时.

                  Hope this helps someone out in the future, because it kept me guessing for a good couple of hours.

                  这篇关于Laravel 多对多 - 意外的结果集 ->select()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使 Laravel 的 Validator $rules 成为可选? 下一篇:laravel eloquent 模型缓存

                  相关文章

                  最新文章

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

                      <legend id='aNdg6'><style id='aNdg6'><dir id='aNdg6'><q id='aNdg6'></q></dir></style></legend>

                      • <bdo id='aNdg6'></bdo><ul id='aNdg6'></ul>

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