all())->where("parent_id",0)->with(['children'])->simplePaginate(); return DepartmentResource::collection($department); } public function store(CreateOrUpdateRequest $request) { $department=new Department(); $department->mergeFillable([ 'company_id' ]); $department->fill([ ...$request->all(), 'company_id' => Auth::user()->company_id, ]); $department->save(); return $this->created(); } public function show(string $id) { $department=Department::query()->findOrFail($id); return new DepartmentResource($department); } public function update(CreateOrUpdateRequest $request,string $id) { $department=Department::findOrFail($id); $department->fill($request->all()); $department->save(); return $this->noContent(); } public function destroy(string $id) { $department = Department::findOrFail($id); if(!empty($department->children()->first())){ throw new \Exception("Please remove the sub-level departments."); } $department->delete(); return $this->noContent(); } }