12345678910111213141516171819202122232425262728293031323334353637 |
- <?php
- namespace App\Http\Resources\API;
- use App\Models\ProjectAsset;
- use Illuminate\Http\Request;
- use Illuminate\Http\Resources\Json\JsonResource;
- class ProjectResource extends JsonResource
- {
- /**
- * Transform the resource into an array.
- *
- * @return array<string, mixed>
- */
- public function toArray(Request $request): array
- {
- $project_ids = ProjectAsset::select('project_id')->distinct()->pluck('project_id')->all();
- return [
- 'id' => $this->id,
- 'name' => $this->name,
- 'code' => $this->code,
- 'status' => $this->status,
- 'const' => $this->const,
- 'begin' => $this->begin,
- 'end' => $this->end,
- 'has_asset' =>in_array($this->id,$project_ids),
- 'asset'=> $this->when($this->assets->isNotEmpty(),function (){
- return $this->assets->map(function ($asset){
- return new SimpleAssetResource($asset);
- })->all();
- }),
- 'created_at'=>(string)$this->created_at,
- //'asset' => new AssetParentResource($this->asset),
- ];
- }
- }
|