我有一个模型资源,我正在调用$this->标签内部
此资源返回如下:
"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']);
有很多收集方法,您正在尝试实现基于此,您可以使用。
拔出
$collection = collect([
[
'speakers' => [
'first_day' => ['Rosa', 'Judith'],
'second_day' => ['Angela', 'Kathleen'],
],
],
]);
$plucked = $collection->pluck('speakers.first_day');
$plucked->all();
// ['Rosa', 'Judith']
仅
$collection = collect(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]);
$filtered = $collection->only(['product_id', 'name']);
$filtered->all();
// ['product_id' => 1, 'name' => 'Desk']
获取
$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);
$value = $collection->get('name');
// Taylor
如果你总是想隐藏一些属性,你可以使用
型号中的protected $hidden = ['parent_id', 'created_at'];