TestUserApiResource.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. if($request->route()->named('someone')) {
  16. return ['someone' => true];
  17. }
  18. $result = [
  19. 'id' => $this->id,
  20. 'name' => $this->first_name . ' ' . $this->last_name,
  21. 'email' => $this->email,
  22. 'children' => $this->whenLoaded('children', function () {
  23. return TestUserApiResource::collection($this->children);
  24. }),
  25. 'pets' => $this->whenLoaded('pets', function () {
  26. return TestPetApiResource::collection($this->pets);
  27. }),
  28. ];
  29. if ($this['state1'] && $this['random-state']) {
  30. $result['state1'] = $this['state1'];
  31. $result['random-state'] = $this['random-state'];
  32. }
  33. return $result;
  34. }
  35. }