PlanResource.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace App\Http\Resources\API;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. class PlanResource 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. 'title' => $this->title,
  17. 'asset_id' => $this->asset_id,
  18. 'parent_id' => $this->parent_id,
  19. 'requirement_total' => $this->requirements()->count(),
  20. 'project_total' => $this->projects()->count(),
  21. 'begin' => $this->begin,
  22. 'end' => $this->end,
  23. 'description' => $this->description,
  24. //'children' => $this->parent_id == 0 ? PlanResource::collection($this->children) : [],
  25. 'children' => $this->when($this->children->isNotEmpty(),function (){
  26. return $this->children->map(function ($child){
  27. return new PlanResource($child);
  28. })->all();
  29. })
  30. ];
  31. }
  32. }