UserResource.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Http\Resources;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  7. use Illuminate\Support\Facades\Storage;
  8. class UserResource extends JsonResource
  9. {
  10. /**
  11. * Transform the resource into an array.
  12. *
  13. * @return array<string, mixed>
  14. */
  15. public function toArray(Request $request): array
  16. {
  17. return [
  18. 'id' => $this->id,
  19. 'name' => $this->name,
  20. 'email' => $this->email,
  21. 'phone' => $this->phone,
  22. 'cpf' => $this->cpf,
  23. 'registration' => $this->registration,
  24. 'language' => $this->language,
  25. 'type' => $this->type,
  26. 'status' => $this->status,
  27. 'admission_date' => $this->admission_date?->format('d/m/Y'),
  28. 'expiry_date' => $this->expiry_date?->format('d/m/Y'),
  29. 'position_id' => $this->position_id,
  30. 'position' => $this->whenLoaded('position', fn() => ['id' => $this->position->id, 'name' => $this->position->name]),
  31. 'sector_id' => $this->sector_id,
  32. 'sector' => $this->whenLoaded('sector', fn() => ['id' => $this->sector->id, 'name' => $this->sector->name]),
  33. 'photo_url' => $this->photo_path
  34. ? Storage::disk('s3')->temporaryUrl($this->photo_path, now()->addHours(24))
  35. : null,
  36. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  37. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  38. ];
  39. }
  40. public static function collection($resource): AnonymousResourceCollection
  41. {
  42. return parent::collection($resource);
  43. }
  44. }