ScheduleResource.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Models\CartItem;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. class ScheduleResource extends JsonResource
  7. {
  8. public function toArray(Request $request): array
  9. {
  10. $cartId = $this->cart_id ?? CartItem::query()
  11. ->where('schedule_id', $this->id)
  12. ->value('cart_id');
  13. $cartItemsCount = $this->cart_items_count ?? (
  14. $cartId ? CartItem::query()->where('cart_id', $cartId)->count() : 0
  15. );
  16. return [
  17. 'id' => $this->id,
  18. 'client_id' => $this->client_id,
  19. 'client_name' => $this->client?->user?->name,
  20. 'provider_id' => $this->provider_id,
  21. 'provider_name' => $this->provider?->user?->name,
  22. 'address_id' => $this->address_id,
  23. 'address_full' => $this->address ? implode(', ', array_filter([
  24. $this->address->address,
  25. $this->address->number ? "nº {$this->address->number}" : null,
  26. $this->address->district,
  27. $this->address->city ? "{$this->address->city->name}/{$this->address->state?->code}" : null,
  28. ])) : null,
  29. 'address' => new AddressResource($this->whenLoaded('address')),
  30. 'date' => $this->date?->format('Y-m-d'),
  31. 'period_type' => $this->period_type,
  32. 'schedule_type' => $this->schedule_type,
  33. 'start_time' => $this->start_time,
  34. 'end_time' => $this->end_time,
  35. 'offers_meal' => $this->offers_meal,
  36. 'status' => $this->status,
  37. 'total_amount' => $this->total_amount,
  38. 'code' => $this->code,
  39. 'code_verified' => $this->code_verified,
  40. 'cart_id' => $cartId,
  41. 'cart_items_count' => $cartItemsCount,
  42. 'created_at' => $this->created_at?->toISOString(),
  43. 'updated_at' => $this->updated_at?->toISOString(),
  44. 'deleted_at' => $this->deleted_at?->toISOString(),
  45. ];
  46. }
  47. }