在我的例子中,我隐藏了我的 Eloquent 领域,如下所示:
public function __construct(array $attributes = array())
{
parent::__construct($attributes);
$user = auth()->user();
$hiddenFields = [];
if(!$user || $user && $user->id !== $this->user_id) {
$hiddenFields = [
'phone', 'employees', 'email',
'created_at', 'updated_at', 'id', 'description'
];
}
$this->hidden = array_merge($this->hidden, $hiddenFields);
}
可以更好地隐藏字段吗?
最佳答案
您可以直接在模型中使用 $visible
或 $hidden
,因此在执行对 ->toArray()
的调用时,它们将不会'无法显示。
文档: https://laravel.com/docs/master/eloquent-serialization#hiding-attributes-from-json
关于laravel - 我如何用更好的方式隐藏 Laravel 模型字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57722284/