Created
September 26, 2017 19:27
-
-
Save deleugpn/dac1888935ef74e4af8b87de105faf56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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