TaskResource.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Http\Resources\API;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. class TaskResource extends JsonResource
  6. {
  7. /**
  8. * Transform the resource into an array.
  9. *
  10. * @return array<string, mixed>
  11. */
  12. public function toArray(Request $request): array
  13. {
  14. return [
  15. "id" => $this->id,
  16. "name" => $this->name,
  17. "parent_id" => $this->parent_id,
  18. "begin" => $this->begin,
  19. "end" => $this->end,
  20. "status" => $this->status,
  21. "assign_to" => new UserProfileResource($this->assignTo),
  22. "created_by" => new UserProfileResource($this->createdBy),
  23. //"children" => TaskResource::collection($this->children),
  24. 'children' => $this->when($this->children->isNotEmpty(),function (){
  25. return $this->children->map(function ($child){
  26. return new TaskResource($child);
  27. })->all();
  28. }),
  29. ];
  30. }
  31. }