AddressResource.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. 'has_complement' => $this->has_complement,
  21. 'complement' => $this->complement,
  22. 'nickname' => $this->nickname,
  23. 'instructions' => $this->instructions,
  24. 'city_id' => $this->city_id,
  25. 'state_id' => $this->state_id,
  26. 'address_full' => "{$this->address}" .
  27. ($this->complement ? " - {$this->complement}" : '') .
  28. ($this->city ? " , {$this->city->name}/{$this->state?->code}" : ''),
  29. 'city' => $this->whenLoaded('city'),
  30. 'state' => $this->whenLoaded('state'),
  31. 'address_type' => $this->address_type,
  32. 'created_at' => $this->created_at,
  33. 'updated_at' => $this->updated_at,
  34. 'deleted_at' => $this->deleted_at,
  35. ];
  36. }
  37. }