UserResource.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Http\Resources;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  6. use Illuminate\Http\Resources\Json\JsonResource;
  7. use Illuminate\Support\Facades\Storage;
  8. class UserResource extends JsonResource
  9. {
  10. public function toArray(Request $request): array
  11. {
  12. return [
  13. 'id' => $this->id,
  14. 'name' => $this->name,
  15. 'cpf' => $this->cpf,
  16. 'email' => $this->email,
  17. 'language' => $this->language,
  18. 'user_type' => $this->user_type,
  19. 'user_type_label' => $this->user_type?->label(),
  20. 'status' => $this->status,
  21. 'state_id' => $this->state_id,
  22. 'unit_id' => $this->whenLoaded('units', fn() => $this->units->first()?->id),
  23. 'avatar_url' => $this->avatar_url
  24. ? Storage::temporaryUrl($this->avatar_url, now()->addHours(24))
  25. : null,
  26. 'last_login_at' => $this->last_login_at
  27. ? Carbon::parse($this->last_login_at)->format('Y-m-d H:i:s')
  28. : null,
  29. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d'),
  30. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d'),
  31. 'state' => $this->whenLoaded('state', fn() => [
  32. 'id' => $this->state->id,
  33. 'name' => $this->state->name,
  34. 'code' => $this->state->code,
  35. ]),
  36. ];
  37. }
  38. public static function collection($resource): AnonymousResourceCollection
  39. {
  40. return parent::collection($resource);
  41. }
  42. }