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();
我对它没有任何问题,因为它返回了预期的数据.大概是这样的:
{
"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
}
]
}
我试图实现但仍然无法实现的是,仅获取关系字段的一个子集即可获得:
{
"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.
这可能吗?