TestUserApiResource.php 873 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Fixtures;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. class TestUserApiResource extends JsonResource
  5. {
  6. /**
  7. * Transform the resource into an array.
  8. *
  9. * @param \Illuminate\Http\Request $request
  10. *
  11. * @return array
  12. */
  13. public function toArray($request)
  14. {
  15. $result = [
  16. 'id' => $this->id,
  17. 'name' => $this->first_name . ' ' . $this->last_name,
  18. 'email' => $this->email,
  19. 'children' => $this->whenLoaded('children', function () {
  20. return TestUserApiResource::collection($this->children);
  21. }),
  22. ];
  23. if ($this['state1'] && $this['random-state']) {
  24. $result['state1'] = $this['state1'];
  25. $result['random-state'] = $this['random-state'];
  26. }
  27. return $result;
  28. }
  29. }