【发布时间】:2020-05-21 17:02:22
【问题描述】:
我的应用程序中有一个产品 API 资源,就像这样
/**
* Transform the resource collection into an array.
*
* @param Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'desc' => $this->desc,
'color' => $this->color,
'amount' => $this->amount,
'available' => $this->available,
'createdAt' => $this->created_at,
'updatedAt' => $this->updated_at,
];
}
我的应用程序中的角色很少,例如管理员、查看者。 当管理员访问 api 时,api 返回所有字段,但是当查看器访问 api 时,它只返回有限的字段。
如何使用Gates & Policies 处理这个问题?
我可以这样做吗
'createdAt' => $this->when($this->authorize('product.list'), $this->created_at)
【问题讨论】:
标签: laravel laravel-api laravel-gate