Laravel多对多 - - 意外结果集 - > select() [英] Laravel Many to many - Unexpected result set on ->select()

查看:162
本文介绍了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多对多 - - 意外结果集 - > select()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆