all())->where("parent_id", 0)->with(['children','asset'=>function($query){ $query->with('parent'); }])->simplePaginate(); return PlanResource::collection($plans); } public function byAssets(Request $request) { $assetIds = $request->get("assets", []); $emptyResponse = $this->success(['data' => []]); if (! $assetIds) { return $emptyResponse; } $assets = Asset::query()->where(function ($query) use ($assetIds) { foreach ($assetIds as $index => $assetId) { $where = $index == 0 ? "where" : "orWhere"; $query->$where("path", "like", "%,". $assetId . ",%"); } })->pluck("id"); if ($assets->isEmpty()) { return $emptyResponse; } $plans = Plan::query()->with(['asset'])->whereIn("asset_id", $assets->toArray())->get(); return PlanByAssetResource::collection($plans); } /** * Store a newly created resource in storage. */ public function store(CreateOrUpdateRequest $request) { $plan = new Plan(); $plan->mergeFillable([ 'company_id' ]); $plan->fill([ ...$request->all(), 'company_id' => Auth::user()->company_id, ]); $plan->save(); return $this->created(); } /** * Display the specified resource. */ public function show(string $id) { $plan = Plan::findOrFail($id); return new PlanResource($plan); } /** * Update the specified resource in storage. */ public function update(CreateOrUpdateRequest $request, string $id) { $plan = Plan::findOrFail($id); $plan->fill($request->all()); $plan->save(); return $this->noContent(); } /** * Remove the specified resource from storage. */ public function destroy(string $id) { $plan = Plan::findOrFail($id); $plan->delete(); return $this->noContent(); } }