我有一个模型资源,我在其中调用$ this->tags
此资源返回如下:
"data": {
"some_keys" : ...,
"some_keys2" : ...,
"tags":[
{
"id": 1,
"name": "c++",
"parent_id": null,
"type": null,
"created_at": "2020-09-27 20:37:57",
"updated_at": "2020-09-27 20:37:57",
"pivot": {
"task_id": 43,
"skill_id": 1
}
}
]
我想做如下:
"data": {
"some_keys" : ...,
"some_keys2" : ...,
"tags": [
{
"id": 1,
"name": "c++"
}
]
我的资源模型:
public function toArray($request)
{
$data = parent::toArray($request);
$data["tags"] = $this->tags;
return $data;
}
我的任务模型:
public function tags(){
return $this->belongsToMany(Skill::class, 'task_skills', 'task_id', 'skill_id');
}
如何减少资源模型中的一些标签列?
我是这样解决的:
$data["tags"] = $this->tags->makeHidden(['pivot','created_at','updated_at','type','parent_id']);