Laravel多对多

编程入门 行业动态 更新时间:2024-10-23 15:27:46
本文介绍了Laravel多对多 - - 意外结果集 - > select()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我不知道有没有人可以帮助,因为我已经打了墙,还在学习Laravel ORM。任何人都可以解释为什么,当我运行:

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

我得到这个结果集:(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多对多

本文发布于:2023-10-25 22:11:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!