UserDependentResource.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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\Support\Facades\Storage;
  7. class UserDependentResource extends JsonResource
  8. {
  9. public function toArray(Request $request): array
  10. {
  11. return [
  12. 'id' => $this->id,
  13. 'responsible_user_id' => $this->responsible_user_id,
  14. 'name' => $this->name,
  15. 'kinship' => $this->kinship,
  16. 'status' => $this->status,
  17. 'document_name' => $this->document_name,
  18. 'document_is_image' => $this->document_path
  19. ? in_array(
  20. strtolower(pathinfo($this->document_path, PATHINFO_EXTENSION)),
  21. ['jpg', 'jpeg', 'png', 'webp'],
  22. true,
  23. )
  24. : false,
  25. 'document_url' => $this->document_path
  26. ? Storage::disk('s3')->temporaryUrl($this->document_path, now()->addHours(24))
  27. : null,
  28. 'responsible_user' => $this->whenLoaded('responsibleUser', fn() => [
  29. 'id' => $this->responsibleUser->id,
  30. 'name' => $this->responsibleUser->name,
  31. 'position' => $this->responsibleUser->relationLoaded('position')
  32. ? $this->responsibleUser->position?->name
  33. : null,
  34. 'sector' => $this->responsibleUser->relationLoaded('sector')
  35. ? $this->responsibleUser->sector?->name
  36. : null,
  37. ]),
  38. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  39. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  40. ];
  41. }
  42. }