{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/8044","labels_url":"https://api.github.com/repos/laravel/framework/issues/8044/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/8044/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/8044/events","html_url":"https://github.com/laravel/framework/issues/8044","id":62624724,"number":8044,"title":"hasMany with select() returns no results","user":{"login":"andreikun","id":11534054,"avatar_url":"https://avatars.githubusercontent.com/u/11534054?v=3","gravatar_id":"","url":"https://api.github.com/users/andreikun","html_url":"https://github.com/andreikun","followers_url":"https://api.github.com/users/andreikun/followers","following_url":"https://api.github.com/users/andreikun/following{/other_user}","gists_url":"https://api.github.com/users/andreikun/gists{/gist_id}","starred_url":"https://api.github.com/users/andreikun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andreikun/subscriptions","organizations_url":"https://api.github.com/users/andreikun/orgs","repos_url":"https://api.github.com/users/andreikun/repos","events_url":"https://api.github.com/users/andreikun/events{/privacy}","received_events_url":"https://api.github.com/users/andreikun/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-03-18T08:34:37Z","updated_at":"2015-03-18T10:12:03Z","closed_at":null,"body":"For $this->hasMany('Model', 'foreign_key') -> indeed Eloquent is returning correct entities but if you try to apply a select condition it does not return anything.\r\n\r\nLet's have an example:\r\n1.I have an album model\r\n2.I have an picture model\r\n3.An album can have multiple pictures and each picture have a name, size, extension and other info\r\n\r\nWhen you try to get an album like http://domain.com/api/album/{album} -> you want also all pictures from that album like :\r\n\r\n    \"id\":\"123\", \r\n    \"name\":\"My Album\", \r\n    \"pictures\":[{\r\n        \"id\":\"1\", \r\n        \"name\":\"Pic1\", \r\n        \"size\":\"1000\"\r\n      },{\r\n        \"id\":\"2\",\r\n         \"name\":\"Pic2\",\r\n         \"size\":\"2000\"\r\n      }]\r\n\r\nAbove is the response when using:\r\n    \r\n     $this->hasMany('App\\Pictures', 'album_id');\r\n\r\nBut in this case i don't want size to be in the response and i don't want to fetch it as array and do extra work to remove my size.\r\n\r\nFor belongsTo condition we can use ->select('column1', 'columns2') option but when comes using it with hasMany, it simply does not work and as a result, the returning data looks like this\r\n\r\n    \"id\":\"123\", \r\n    \"name\":\"My Album\", \r\n    \"pictures\":[]\r\n\r\nAbove is the response when using:\r\n\r\n    $this->hasMany('App\\Pictures', 'album_id')->select('id', 'name');\r\n\r\nInstead of :\r\n\r\n    \"id\":\"123\", \r\n    \"name\":\"My Album\", \r\n    \"pictures\":[{\r\n        \"id\":\"1\", \r\n        \"name\":\"Pic1\"\r\n     },{\r\n        \"id\":\"2\",\r\n         \"name\":\"Pic2\"\r\n     }]"},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/82870138","html_url":"https://github.com/laravel/framework/issues/8044#issuecomment-82870138","issue_url":"https://api.github.com/repos/laravel/framework/issues/8044","id":82870138,"user":{"login":"andreikun","id":11534054,"avatar_url":"https://avatars.githubusercontent.com/u/11534054?v=3","gravatar_id":"","url":"https://api.github.com/users/andreikun","html_url":"https://github.com/andreikun","followers_url":"https://api.github.com/users/andreikun/followers","following_url":"https://api.github.com/users/andreikun/following{/other_user}","gists_url":"https://api.github.com/users/andreikun/gists{/gist_id}","starred_url":"https://api.github.com/users/andreikun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andreikun/subscriptions","organizations_url":"https://api.github.com/users/andreikun/orgs","repos_url":"https://api.github.com/users/andreikun/repos","events_url":"https://api.github.com/users/andreikun/events{/privacy}","received_events_url":"https://api.github.com/users/andreikun/received_events","type":"User","site_admin":false},"created_at":"2015-03-18T10:12:03Z","updated_at":"2015-03-18T10:12:03Z","body":"Hmm, interesting. I never thought that if you do not include the id, it won't fetch you any data. It seems that the same thing happens when you do not include \"id\" column in belongsTo relation. \r\n\r\nThere i had $this->belongsTo('App\\Album')->select('id', 'name'), but i included it because i needed it. When getting Pictures from Albums is supposed that i already know about album_id.\r\n\r\nWeird though. They should put some more documentation on the website, or somewhere to be able to read about this."}}