隐藏枢轴属性

问题描述 投票:0回答:1

我知道你可以隐藏整个数据透视表

protected $hidden = ['pivot'];

如何隐藏数据透视表中的特定字段,例如

protected $hidden = ['pivot.created_at'];

根据我的测试,上述方法不起作用

php laravel laravel-5
1个回答
6
投票

经过如此多的尝试和研究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;
}

这样就解决了目的。

© www.soinside.com 2019 - 2024. All rights reserved.