CustomScheduleResource.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. use Illuminate\Support\Facades\Storage;
  6. class CustomScheduleResource extends JsonResource
  7. {
  8. /**
  9. * Transform the resource into an array.
  10. *
  11. * @return array<string, mixed>
  12. */
  13. public function toArray(Request $request): array
  14. {
  15. return [
  16. 'id' => $this->id,
  17. 'schedule_id' => $this->schedule_id,
  18. 'address_type' => $this->address_type,
  19. 'service_type_id' => $this->service_type_id,
  20. 'service_type_name' => $this->whenLoaded('serviceType', function () {
  21. return $this->serviceType->description;
  22. }),
  23. 'description' => $this->description,
  24. 'min_price' => number_format($this->min_price, 2, '.', ''),
  25. 'max_price' => number_format($this->max_price, 2, '.', ''),
  26. 'offers_meal' => $this->offers_meal,
  27. 'created_at' => $this->created_at?->format('Y-m-d H:i'),
  28. 'updated_at' => $this->updated_at?->format('Y-m-d H:i'),
  29. 'schedule' => $this->whenLoaded('schedule', function () {
  30. return [
  31. 'id' => $this->schedule->id,
  32. 'client_id' => $this->schedule->client_id,
  33. 'provider_id' => $this->schedule->provider_id,
  34. 'client_name' => $this->schedule->client?->user?->name,
  35. 'customer_photo' => (function () {
  36. $path = $this->schedule->client?->profileMedia?->path;
  37. return $path ? Storage::temporaryUrl($path, now()->addMinutes(60)) : null;
  38. })(),
  39. 'address_id' => $this->schedule->address_id,
  40. 'address' => $this->schedule->address ? [
  41. 'address' => $this->schedule->address->address,
  42. 'number' => $this->schedule->address->number,
  43. 'district' => $this->schedule->address->district,
  44. 'complement' => $this->schedule->address->complement,
  45. 'zip_code' => $this->schedule->address->zip_code,
  46. 'city' => $this->schedule->address->city,
  47. 'state' => $this->schedule->address->state,
  48. 'address_full' => implode(', ', array_filter([
  49. $this->schedule->address->address,
  50. $this->schedule->address->number ? "nº {$this->schedule->address->number}" : null,
  51. $this->schedule->address->district,
  52. $this->schedule->address->city ? "{$this->schedule->address->city->name}/{$this->schedule->address->state?->code}" : null,
  53. ])),
  54. ] : null,
  55. 'date' => $this->schedule->date?->format('d/m/Y'),
  56. 'period_type' => $this->schedule->period_type,
  57. 'start_time' => $this->schedule->start_time,
  58. 'end_time' => $this->schedule->end_time,
  59. 'status' => $this->schedule->status,
  60. ];
  61. }),
  62. 'specialities' => $this->whenLoaded('specialities', function () {
  63. return $this->specialities->map(function ($item) {
  64. return [
  65. 'id' => $item->id,
  66. 'name' => $item->description,
  67. ];
  68. });
  69. }),
  70. ];
  71. }
  72. }