ProjectResource.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Http\Resources\API;
  3. use App\Models\ProjectAsset;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. class ProjectResource 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. $project_ids = ProjectAsset::select('project_id')->distinct()->pluck('project_id')->all();
  16. return [
  17. 'id' => $this->id,
  18. 'name' => $this->name,
  19. 'code' => $this->code,
  20. 'status' => $this->status,
  21. 'const' => $this->const,
  22. 'begin' => $this->begin,
  23. 'end' => $this->end,
  24. 'has_asset' =>in_array($this->id,$project_ids),
  25. 'asset'=> $this->when($this->assets->isNotEmpty(),function (){
  26. return $this->assets->map(function ($asset){
  27. return new SimpleAssetResource($asset);
  28. })->all();
  29. }),
  30. 'created_at'=>(string)$this->created_at,
  31. //'asset' => new AssetParentResource($this->asset),
  32. 'approval_status'=>$this->approval_status
  33. ];
  34. }
  35. }