php - Laravel - hasManyThrough and eager loading, get column from related -
i'm getting weird stuff going on when trying eager load related model using hasmanythrough.
i have community model has function called 'roommates' users have community id in column called 'community', see below code.
public function roommates() { return $this->hasmanythrough('app\user', 'app\community', 'id', 'community'); }
then user model grabbed auth after login , eager loads related models, , displaying columns want. id, name, email
$user = user::where('id', auth::id())->with(['community.roommates' => function ($query) { $query->select('users.id', 'name', 'email'); }])->first();
however json response isn't expected, id being returned appears id of community, though have specified 'users.id'
{ "status": true, "data": { "id": 3, "name": "foo bar", "email": "test@test.com", "community": { "id": 1, "owner": 3, "title": "home", "created_at": "2015-10-09 08:04:05", "updated_at": "2015-10-09 08:04:05", "roommates": [ { "id": 1, "name": "foo bar 2", "email": "test@test.com" }, { "id": 1, "name": "foo bar", "email": "test@test.com" } ] }, "remember_token": "tprkinoybbiviwqkvcjmcwu8poz1/00uktmy7aq+", "created_at": "2015-10-09 08:03:42", "updated_at": "2015-10-10 04:56:14" } }
as can see id on both users in roommates array 1, ids of users 2 , 3.
any ideas?
i'm still feeling around laravel i'm not sure go here?
ending working out,
i changed use hasmany instead of hasmanythrough
public function roommates() { return $this->hasmany('app\user', 'community_id', 'id')->select(['community_id', 'id', 'name', 'email']); }
and needed select foreign key, community_id, or else blank result.
credits question helping me figure out
Comments
Post a Comment