all())->where("parent_id", 0)->where('company_id',Auth::user()->company_id)->with(['children','children.children'])->simplePaginate(); return AssetResource::collection($assets); } /** * Store a newly created resource in storage. */ public function store(CreateOrUpdateRequest $request) { Asset::create([ ...$request->all(), 'company_id' => Auth::user()->company_id, 'whitelist' => $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null, 'created_by' => Auth::id(), ]); 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); $asset->update([ ...$request->all(), 'whitelist' => $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null, ]); 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'])->findOrFail($id); return new AssetReportResource($asset); } }