UserResource.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. 'unread_notifications_count' => (int) ($this->unread_notifications_count ?? 0),
  37. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  38. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  39. ];
  40. }
  41. public static function collection($resource): AnonymousResourceCollection
  42. {
  43. return parent::collection($resource);
  44. }
  45. }