I have a hasMany relation function like this:
public function articles()
{
return $this->hasMany('App\Article');
}
And use it like this:
$data = \App\User::with('articles')->get();
I don't have any problems with it, since it's returning the expected data. Something like this:
{
"id": 1,
"name": "Jhon",
"lastname": "Doe",
"articles": [
{
"id": 1,
"title": "Article 1",
"status": "published",
"published_at": "2015-04-30"
},
{
"id": 2,
"title": "Article 2",
"status": "draft",
"published_at": null
}
]
}
What I am trying to achieve but I still can't it's to fetch just a subset of the relation's fields to obtain this:
{
"id": 1,
"name": "Jhon",
"lastname": "Doe",
"articles": [
{
"id": 1,
"title": "Article 1"
},
{
"id": 2,
"title": "Article 2"
}
]
}
My intention is to find a way to specify the subset of fields in the Model's function instead of iterating the returning collection and unset the unwanted fields.
Is this possible?