我知道你可以隐藏整个数据透视表
protected $hidden = ['pivot'];
如何隐藏数据透视表中的特定字段,例如
protected $hidden = ['pivot.created_at'];
根据我的测试,上述方法不起作用
经过如此多的尝试和研究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;
}
这样就解决了目的。