TestPetApiResource.php 953 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Fixtures;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use Knuckles\Scribe\Attributes\ResponseField;
  5. class TestPetApiResource extends JsonResource
  6. {
  7. /**
  8. * Transform the resource into an array.
  9. *
  10. * @param \Illuminate\Http\Request $request
  11. *
  12. * @return array
  13. */
  14. #[ResponseField('id', description: 'The id of the pet.')]
  15. #[ResponseField('species', 'string', 'The breed')]
  16. public function toArray($request)
  17. {
  18. $result = [
  19. 'id' => $this->id,
  20. 'name' => $this->name,
  21. 'species' => $this->species,
  22. 'owners' => $this->whenLoaded('owners', function () {
  23. return TestUserApiResource::collection($this->owners);
  24. }),
  25. 'ownership' => $this->whenPivotLoaded('pet_user', function () {
  26. return $this->pivot;
  27. })
  28. ];
  29. return $result;
  30. }
  31. }