I have a model resource and I am calling $this->tags inside of it
This resource returns as the following:
"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
}
}
]
I want to do as follows:
"data": {
"some_keys" : ...,
"some_keys2" : ...,
"tags": [
{
"id": 1,
"name": "c++"
}
]
My resources Model:
public function toArray($request)
{
$data = parent::toArray($request);
$data["tags"] = $this->tags;
return $data;
}
My Task Model:
public function tags(){
return $this->belongsToMany(Skill::class, 'task_skills', 'task_id', 'skill_id');
}
How can I reduce some columns of tags in the resources model?
I solved it this way:
$data["tags"] = $this->tags->makeHidden(['pivot','created_at','updated_at','type','parent_id']);
There are quite a few collection methods, which you are trying to achieve based on that you can use.
pluck
only
get
If you always want to keeps some attributes hidden you can use
protected $hidden = ['parent_id', 'created_at'];
in your model