ProjectResource.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. 'cost_unit'=>$this->cost_unit,
  34. 'display_id'=>$this->display_id,
  35. ];
  36. }
  37. }