我有一个 hasManythrough 关系。如下:
public function contacts(){
return $this->hasManyThrough(ContactContent::class, Content::class,"id",'as_parent_content_id','as_parent_content_id','id');
}
public function getSupportElementsAttribute(){
return [
"contacts" => $this->contacts,
"documents" => $this->documents,
"assignments" => $this->assignments
];
}
Run Code Online (Sandbox Code Playgroud)
这个返回是这样的:
"contacts": [
{
"id": 66,
"as_parent_content_id": 5074,
"created_at": "2020-09-30 16:21:11",
"updated_at": "2020-09-30 16:21:11",
"create_user_id": 1,
"laravel_through_key": 5074
}
],
Run Code Online (Sandbox Code Playgroud)
如何删除这个 laravel_through_key ?
您可能想从集合中隐藏此列吗?然后 :
$user = User::with('posts')->get();
$user->makeHidden('laravel_through_key');
Run Code Online (Sandbox Code Playgroud)
现在laravel_through_key将从您的收藏中隐藏。
替代解决方案:添加laravel_through_key模型的$hidden属性,如:
protected $hidden = [
'laravel_through_key'
];
Run Code Online (Sandbox Code Playgroud)
您可以向模型添加受保护的属性
参考链接https://laravel.com/docs/8.x/eloquent-serialization#hiding-attributes-from-json
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'laravel_through_key'
];
Run Code Online (Sandbox Code Playgroud)
或者你可以使用 makeHidden()
参考链接https://laravel.com/docs/8.x/eloquent-collections#method-makeHidden
public function getSupportElementsAttribute(){
return [
"contacts" => $this->contacts->makeHidden('laravel_through_key'),
"documents" => $this->documents,
"assignments" => $this->assignments
];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1058 次 |
| 最近记录: |