我得到这个结果集:(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 b $ b {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 deleted_at:null }, {user_id:1,location_tag_id:2,id 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 } ] }哪个是ace!但是,当我开始从location_tags加入中选择我想要的列时,使用:
public function locationTags(){ return $ this-> hasMany('App\UserHasLocationTags','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:[] }有人可以解释发生了什么吗?并且可能指向正确的方向限制选择?谢谢!
更新 我也试过:
$ query = \App\User :: with(['permissions','locationTags'=> function($ query){ $ query-> select 'lt.id','lt.name'); }]);哪个返回相同的结果:(
解决方案此处的关键是您必须包含一个 select() Laravel可以使用至少一个键来映射结果集,在我的情况下,它是 user_id ,如下所示:
public function locationTags(){ return $ this-> hasMany('App\UserHasLocationTags','user_id') - > join('location_tags ('user_id','lt.name','location_tag_id'); }然后返回一个更好的结果集:
{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 } ] }希望这有助于有人在未来,因为它让我猜测好几个小时。
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('App\UserHasLocationTags', '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 } ] }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('App\UserHasLocationTags', 'user_id') ->join('location_tags AS lt', 'lt.id', '=', 'location_tag_id') ->select('lt.id', 'lt.name'); }I end up with:
{ "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!
Update I've also tried:
$query = \App\User::with(['permissions', 'locationTags' => function($query){ $query->select('lt.id', 'lt.name'); }]);Which returns the same result :(
解决方案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('App\UserHasLocationTags', '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多对多
发布评论