UnitResource.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Http\Resources;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  7. use Illuminate\Support\Facades\Storage;
  8. use App\Models\Unit;
  9. class UnitResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * @return array<string, mixed>
  15. */
  16. public function toArray(Request $request): array
  17. {
  18. return [
  19. 'id' => $this->id,
  20. 'fantasy_name' => $this->fantasy_name,
  21. 'social_reason' => $this->social_reason,
  22. 'cnpj' => $this->cnpj,
  23. 'state_registration' => $this->state_registration,
  24. 'name_responsible' => $this->name_responsible,
  25. 'street' => $this->street,
  26. 'address_number' => $this->address_number,
  27. 'postal_code' => $this->postal_code,
  28. 'neighborhood' => $this->neighborhood,
  29. 'complement' => $this->complement,
  30. 'city_id' => $this->city_id,
  31. 'state_id' => $this->state_id,
  32. 'email' => $this->email,
  33. 'secondary_email' => $this->secondary_email,
  34. 'phone_number' => $this->phone_number,
  35. 'cell_number' => $this->cell_number,
  36. 'avatar_url' => $this->avatar_url
  37. ? Storage::url($this->avatar_url)
  38. : null,
  39. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  40. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  41. 'city' => $this->whenLoaded('city', fn() => [
  42. 'id' => $this->city->id,
  43. 'name' => $this->city->name,
  44. ]),
  45. 'state' => $this->whenLoaded('state', fn() => [
  46. 'id' => $this->state->id,
  47. 'name' => $this->state->name,
  48. 'code' => $this->state->code,
  49. ]),
  50. ];
  51. }
  52. /**
  53. * @param \Illuminate\Database\Eloquent\Collection<Unit> $resource
  54. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<UnitResource>
  55. */
  56. public static function collection($resource): AnonymousResourceCollection
  57. {
  58. return parent::collection($resource);
  59. }
  60. }