AddressResource.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. class AddressResource extends JsonResource
  6. {
  7. /**
  8. * Transform the resource into an array.
  9. *
  10. * @return array<string, mixed>
  11. */
  12. public function toArray(Request $request): array
  13. {
  14. return [
  15. 'id' => $this->id,
  16. 'source' => $this->source,
  17. 'source_id' => $this->source_id,
  18. 'zip_code' => $this->zip_code,
  19. 'address' => $this->address,
  20. 'number' => $this->number,
  21. 'district' => $this->district,
  22. 'has_complement' => $this->has_complement,
  23. 'complement' => $this->complement,
  24. 'nickname' => $this->nickname,
  25. 'instructions' => $this->instructions,
  26. 'city_id' => $this->city_id,
  27. 'state_id' => $this->state_id,
  28. 'address_full' => implode(', ', array_filter([
  29. $this->address,
  30. $this->number ? "nº {$this->number}" : null,
  31. $this->district,
  32. $this->city ? "{$this->city->name}/{$this->state?->code}" : null,
  33. ])),
  34. 'city' => $this->whenLoaded('city'),
  35. 'state' => $this->whenLoaded('state'),
  36. 'address_type' => $this->address_type,
  37. 'created_at' => $this->created_at,
  38. 'updated_at' => $this->updated_at,
  39. 'deleted_at' => $this->deleted_at,
  40. ];
  41. }
  42. }