ReviewResource.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 ReviewResource extends JsonResource
  7. {
  8. public function toArray(Request $request): array
  9. {
  10. return [
  11. 'id' => $this->id,
  12. 'schedule_id' => $this->schedule_id,
  13. 'schedule_label' => $this->schedule
  14. ? ($this->schedule->id.' - '.
  15. ($this->schedule->client?->user?->name ?? '?').' - '.
  16. ($this->schedule->provider?->user?->name ?? '?').' - '.
  17. ($this->schedule->date?->format('d/m/Y') ?? '?'))
  18. : null,
  19. 'origin' => $this->origin,
  20. 'origin_id' => $this->origin_id,
  21. 'stars' => $this->stars,
  22. 'comment' => $this->comment,
  23. 'reviews_improvements' => $this->whenLoaded('reviewsImprovements', function () {
  24. return $this->reviewsImprovements->map(function ($type) {
  25. return [
  26. 'id' => $type->improvementType->id,
  27. 'description' => $type->improvementType->description,
  28. ];
  29. });
  30. }),
  31. 'photos' => $this->whenLoaded('reviewMedia', function () {
  32. return $this->reviewMedia->map(fn ($rm) => [
  33. 'id' => $rm->media_id,
  34. 'origin' => $rm->origin,
  35. 'url' => $rm->media?->path
  36. ? Storage::temporaryUrl($rm->media->path, now()->addMinutes(60))
  37. : null,
  38. ]);
  39. }),
  40. 'created_at' => $this->created_at?->format('Y-m-d H:i'),
  41. 'updated_at' => $this->updated_at?->format('Y-m-d H:i'),
  42. ];
  43. }
  44. }