Laravel5.5 API 资源 如何做 dingo API 的 Transformer ?
laravel5.5 API资源 如何做dingo API的 Transformer
类似Transformer 这种可以对返回的数据字段单个处理并且引用Model 中的方法
文档里面的这种
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'posts' => Post::collection($this->whenLoaded('posts')),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
好像不可以引用数据集
而对数据集的操作 文档只有
public function toArray($request)
{
return [
'data' => $this->collection,
'links' => [
'self' => 'link-value',
],
];
}
但我是想对数据集中单个字段进行控制,并且可以随意添加删除返回的字段 ,不完全依赖数据库所返回的数据
对指定字段值加密,指定字段从缓存中获取数据 等等操作。
所以这个问题 ,有大牛可以解答一下吗?
推荐文章: