在 Laravel 中动态隐藏 API 字段的方法(2)

<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class UsersResource extends Resource
{
 /**
  * @var array
  */
 protected $withoutFields = [];
 
  /**
  * Transform the resource into an array.
  *
  * @param \Illuminate\Http\Request
  * @return array
  */
 public function toArray($request)
 {
  return $this->filterFields([
   'id' => $this->id,
   'name' => $this->name,
   'email' => $this->email
  ]);
 }
 /**
  * Set the keys that are supposed to be filtered out.
  *
  * @param array $fields
  * @return $this
  */
 public function hide(array $fields)
 {
  $this->withoutFields = $fields;
  return $this;
 }
 /**
  * Remove the filtered keys.
  *
  * @param $array
  * @return array
  */
 protected function filterFields($array)
 {
  return collect($array)->forget($this->withoutFields)->toArray();
 }
}

大功告成! 现在我们可以访问 http://api.dev/api/users/1 ,你会发现响应中已经没有id 字段了。

{
 "data": {
 "name": "Mr. Frederik Morar",
 "email": "darryl.wilkinson@example.org"
 }
}

5- UsersResourceCollection 类

执行项目集合中的 index 方法, 我们需要作出如下修改:

(1) 确保  UsersResource::collection 返回 UsersResourceCollection 实例

(2) 在  UsersResourceCollection 上公开 hide 方法

(3) 将隐藏的字段传递给  UsersResource

关于 (1), 我们只需要重写  UsersResource 中的 collection 方法

<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class UsersResource extends Resource
{
 public static function collection($resource)
 {
  return tap(new UsersResourceCollection($resource), function ($collection) {
   $collection->collects = __CLASS__;
  });
 }
 
 /**
  * @var array
  */
 protected $withoutFields = [];
 /**
  * Transform the resource into an array.
  * 将资源转换为一个数组
  * 
  * @param \Illuminate\Http\Request
  * @return array
  */
 public function toArray($request)
 {
  return $this->filterFields([
   'id' => $this->id,
   'name' => $this->name,
   'email' => $this->email
  ]);
 }
 /**
  * Set the keys that are supposed to be filtered out.
  * 设置需要隐藏过滤掉的键
  * 
  * @param array $fields
  * @return $this
  */
 public function hide(array $fields)
 {
  $this->withoutFields = $fields;
  return $this;
 }
 /**
  * Remove the filtered keys.
  * 删除隐藏的键
  * 
  * @param $array
  * @return array
  */
 protected function filterFields($array)
 {
  return collect($array)->forget($this->withoutFields)->toArray();
 }
}

关于 (2) 和 (3) 我们需要修改 UsersResourceCollection 文件. 让我们公开 hide 方法并使用隐藏字段处理集合。.

<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class UsersResourceCollection extends ResourceCollection
{
 /**
  * @var array
  */
 protected $withoutFields = [];
 /**
  * Transform the resource collection into an array.
  *
  * @param \Illuminate\Http\Request
  * @return array
  */
 public function toArray($request)
 {
  return $this->processCollection($request);
 }
 public function hide(array $fields)
 {
  $this->withoutFields = $fields;
  return $this;
 }
 /**
  * Send fields to hide to UsersResource while processing the collection.
  * 将隐藏字段通过 UsersResource 处理集合
  * 
  * @param $request
  * @return array
  */
 protected function processCollection($request)
 {
  return $this->collection->map(function (UsersResource $resource) use ($request) {
   return $resource->hide($this->withoutFields)->toArray($request);
  })->all();
 }
}
      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3925.html