get('page_size') ?? 10; $assets = Asset::filter($request->all()) ->where("parent_id", 0) ->where('company_id',Auth::user()->company_id) ->with(['children','children.children']) ->paginate($pageSize); return AssetResource::collection($assets); } /** * Store a newly created resource in storage. */ public function store(CreateOrUpdateRequest $request) { $asset = Asset::create([ ...$request->all(), 'company_id' => Auth::user()->company_id, 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null, 'created_by' => Auth::id(), ]); $parentAsset = $request->parent_id > 0 ? Asset::query()->findOrFail($request->parent_id) : null; $asset->path = $parentAsset ? $parentAsset?->path . $asset->id . "," : sprintf(",%s,", $asset->id); $asset->save(); return $this->created(); } /** * Display the specified resource. */ public function show(string $id) { $asset = Asset::allowed()->findOrFail($id); return new AssetResource($asset); } /** * Update the specified resource in storage. */ public function update(Request $request, string $id) { $asset = Asset::allowed()->findOrFail($id); $parentAsset = $request->parent_id > 0 ? Asset::query()->findOrFail($request->parent_id) : null; $isUpdateParent = $parentAsset?->id != $asset->parent_id; $path = $isUpdateParent ? $parentAsset->path . $asset->id . "," : $asset->path; $formData = [ ...$request->all(), 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null, 'path' => $path ]; if ($isUpdateParent) { $children = Asset::query() ->where("id", "!=", $asset->id) ->where("path", "like", "%," . $asset->id . ",%") ->get(); foreach ($children as $child) { $child->fill([ 'path' => str_replace($asset->path, $path, $child->path), ]); $child->save(); } } $asset->fill($formData); $asset->save(); return $this->noContent(); } /** * Remove the specified resource from storage. */ public function destroy(string $id) { $asset = Asset::allowed()->findOrFail($id); if(!empty($asset->children()->first())){ throw new \Exception("Please remove the sub-level assets."); } $asset->delete(); return $this->noContent(); } public function report(string $id){ $asset = Asset::allowed()->with(['requirements','plans','projects','children'])->findOrFail($id); //$asset = Asset::find($id); $assets=Asset::query()->where("path","like","%,".$id.",%")->pluck("id"); $asset->setAttribute('child_id',$assets); return new AssetReportResource($asset); } }