PlanResource.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. 'asset_name' => $this->asset ? $this->asset->name : null,
  19. 'parent_id' => $this->parent_id,
  20. 'requirement_total' => $this->requirements()->count(),
  21. 'project_total' => $this->projects()->count(),
  22. 'begin' => $this->begin,
  23. 'end' => $this->end,
  24. 'description' => $this->description?(new \App\Services\File\ImageUrlService)->getImageUrl($this->description):null,
  25. //'children' => $this->parent_id == 0 ? PlanResource::collection($this->children) : [],
  26. 'children' => $this->when($this->children->isNotEmpty(),function (){
  27. return $this->children->map(function ($child){
  28. return new PlanResource($child);
  29. })->all();
  30. }),
  31. 'asset' => new AssetParentResource($this->asset),
  32. ];
  33. }
  34. }