RequirementGroupResource.php 1002 B

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