ClassPackageUnitResource.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Http\Resources;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. class ClassPackageUnitResource extends JsonResource
  7. {
  8. public function toArray(Request $request): array
  9. {
  10. return [
  11. 'id' => $this->id,
  12. 'unit_id' => $this->unit_id,
  13. 'class_package_id' => $this->class_package_id,
  14. 'name' => $this->name,
  15. 'quantity_classes' => $this->quantity_classes,
  16. 'contract_material_value' => $this->contract_material_value,
  17. 'visible' => $this->visible,
  18. 'class_duration_minutes' => $this->class_duration_minutes,
  19. 'weekday' => $this->weekday,
  20. 'start_time' => $this->start_time,
  21. 'second_weekday' => $this->second_weekday,
  22. 'second_start_time' => $this->second_start_time,
  23. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  24. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  25. 'materials' => $this->whenLoaded('products', fn() =>
  26. $this->products->map(fn($item) => [
  27. 'product_id' => $item->product_id,
  28. 'name' => $item->product?->name,
  29. 'quantity' => $item->quantity,
  30. 'price' => (float) $item->price,
  31. ])
  32. ),
  33. 'variations' => ClassPackageUnitVariationResource::collection($this->whenLoaded('variations')),
  34. ];
  35. }
  36. }