KanbanResource.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  6. use App\Models\Kanban;
  7. class KanbanResource extends JsonResource
  8. {
  9. public function toArray(Request $request): array
  10. {
  11. return [
  12. 'id' => $this->id,
  13. 'title' => $this->title,
  14. 'description' => $this->description,
  15. 'phase' => $this->phase,
  16. 'order' => $this->order,
  17. 'priority' => $this->priority,
  18. 'origin' => $this->origin,
  19. 'scope' => $this->scope,
  20. 'sector' => $this->sector,
  21. 'due_date' => $this->due_date?->format('Y-m-d'),
  22. 'responsible_user_id' => $this->responsible_user_id,
  23. 'created_by_user_id' => $this->created_by_user_id,
  24. 'unit_id' => $this->unit_id,
  25. 'target_unit_id' => $this->target_unit_id,
  26. // Resolved names for the UI
  27. 'created_by_user_name' => $this->whenLoaded('createdByUser', fn() => $this->createdByUser?->name),
  28. 'responsible_user_name'=> $this->whenLoaded('responsibleUser', fn() => $this->responsibleUser?->name),
  29. 'applicant_unit_name' => $this->whenLoaded('applicantUnit', fn() => $this->applicantUnit?->fantasy_name),
  30. 'target_unit_name' => $this->whenLoaded('targetUnit', fn() => $this->targetUnit?->fantasy_name),
  31. 'replies_count' => $this->whenCounted('replies'),
  32. 'created_at' => $this->created_at?->format('d/m/Y H:i'),
  33. 'updated_at' => $this->updated_at?->format('d/m/Y H:i'),
  34. ];
  35. }
  36. /**
  37. * @param \Illuminate\Database\Eloquent\Collection<Kanban> $resource
  38. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<KanbanResource>
  39. */
  40. public static function collection($resource): AnonymousResourceCollection
  41. {
  42. return parent::collection($resource);
  43. }
  44. }