Skip to content

Instantly share code, notes, and snippets.

@deleugpn
Created September 26, 2017 19:27
Show Gist options
  • Select an option

  • Save deleugpn/dac1888935ef74e4af8b87de105faf56 to your computer and use it in GitHub Desktop.

Select an option

Save deleugpn/dac1888935ef74e4af8b87de105faf56 to your computer and use it in GitHub Desktop.
<?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.
*
* @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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment