CustomScheduleResource.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. class CustomScheduleResource 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. 'schedule_id' => $this->schedule_id,
  17. 'address_type' => $this->address_type,
  18. 'service_type_id' => $this->service_type_id,
  19. 'service_type_name' => $this->whenLoaded('serviceType', function () {
  20. return $this->serviceType->description;
  21. }),
  22. 'description' => $this->description,
  23. 'min_price' => number_format($this->min_price, 2, '.', ''),
  24. 'max_price' => number_format($this->max_price, 2, '.', ''),
  25. 'offers_meal' => $this->offers_meal,
  26. 'created_at' => $this->created_at?->format('Y-m-d H:i'),
  27. 'updated_at' => $this->updated_at?->format('Y-m-d H:i'),
  28. 'schedule' => $this->whenLoaded('schedule', function () {
  29. return [
  30. 'id' => $this->schedule->id,
  31. 'client_id' => $this->schedule->client_id,
  32. 'provider_id' => $this->schedule->provider_id,
  33. 'client_name' => $this->schedule->client?->user?->name,
  34. 'address_id' => $this->schedule->address_id,
  35. 'address' => $this->schedule->address ? [
  36. 'address' => $this->schedule->address->address,
  37. 'number' => $this->schedule->address->number,
  38. 'district' => $this->schedule->address->district,
  39. 'complement' => $this->schedule->address->complement,
  40. 'zip_code' => $this->schedule->address->zip_code,
  41. 'city' => $this->schedule->address->city,
  42. 'state' => $this->schedule->address->state,
  43. 'address_full' => implode(', ', array_filter([
  44. $this->schedule->address->address,
  45. $this->schedule->address->number ? "nº {$this->schedule->address->number}" : null,
  46. $this->schedule->address->district,
  47. $this->schedule->address->city ? "{$this->schedule->address->city->name}/{$this->schedule->address->state?->code}" : null,
  48. ])),
  49. ] : null,
  50. 'date' => $this->schedule->date?->format('d/m/Y'),
  51. 'period_type' => $this->schedule->period_type,
  52. 'start_time' => $this->schedule->start_time,
  53. 'end_time' => $this->schedule->end_time,
  54. 'status' => $this->schedule->status,
  55. ];
  56. }),
  57. 'specialities' => $this->whenLoaded('specialities', function () {
  58. return $this->specialities->map(function ($item) {
  59. return [
  60. 'id' => $item->id,
  61. 'name' => $item->description,
  62. ];
  63. });
  64. }),
  65. ];
  66. }
  67. }