我知道你可以隐藏整个数据透视表
protected $hidden = ['pivot'];
Run Code Online (Sandbox Code Playgroud)
如何隐藏数据透视表中的特定字段,如
protected $hidden = ['pivot.created_at'];
Run Code Online (Sandbox Code Playgroud)
以上不适用于我测试过的内容
经过如此多的尝试和研究Laravel 模型的来源,我终于实现了。
请在您的模型中使用以下方法。
/**
* Convert the model instance to an array.
*
* @return array
*/
public function toArray()
{
$attributes = $this->attributesToArray();
$attributes = array_merge($attributes, $this->relationsToArray());
unset($attributes['pivot']['created_at']);
return $attributes;
}
Run Code Online (Sandbox Code Playgroud)
这就解决了目的。