osRepository->filter([ 'page' => $request->get('page') ?? 1 ], ['osGroup.host']); $title = 'List all Version OS'; return view('administrator.pages.package.version.index', compact('versions', 'title')); } public function create() { $title = 'Add new Version Os'; return view('administrator.pages.package.version.add', compact('title')); } public function store(OsVersionRequest $request) { $validatedData = $request->validated(); unset($validatedData['module_datacenter_id']); DB::beginTransaction(); try { $this->osRepository->create($validatedData); DB::commit(); } catch (\Exception $error) { DB::rollBack(); return back()->withErrors($error->getMessage()); } return redirect()->route('package.version.index')->with('success', 'Add Os Version successfully'); } public function edit(int $id = 0) { $version = $this->osRepository->findWithRelationships($id, ['osGroup.host.datacenter.vCenter']); if (! $version) { return back()->withErrors('Can\'t find version os by ID #' . $id); } $title = "Edit version OS"; return view('administrator.pages.package.version.edit', compact('title', 'version')); } public function update(int $id = 0, OsVersionRequest $request) { $version = $this->osRepository->find($id); if (! $version) { return back()->withErrors('Can\'t find version os by ID #' . $id); } $validatedData = $request->validated(); // Remove unnecessary fields unset($validatedData['vcenter_id'], $validatedData['module_datacenter_id']); DB::beginTransaction(); try { $this->osRepository->update($id, $validatedData); DB::commit(); } catch (\Exception $error) { DB::rollBack(); return back()->withErrors($error->getMessage()); } return redirect()->route('package.version.index')->with('success', 'Update successfully'); } public function destroy($id) { try { $version = $this->osRepository->find($id); if (!$version) { return back()->withErrors('Can\'t find version os by ID #' . $id); } $version->delete(); return redirect()->route('package.version.index') ->with('success', 'OS Version deleted successfully'); } catch (\Exception $error) { return back()->withErrors('Failed to delete OS Version: ' . $error->getMessage()); } } public function datatable(Request $request) { $query = $this->osRepository->filter( [], ['osGroup'], [], ['*'], true ); return DataTables::eloquent($query) ->editColumn('os_group', function ($d) { return $d->osGroup ? $d->osGroup->name . ' - ' . $d->osGroup->host->name : ''; }) ->editColumn('status', function ($d) { return $d->getStatus(); }) ->addColumn('action', function ($d) { return view('administrator.pages.package.version.datatable.action', [ 'id' => $d->id, 'version' => $d->version ])->render(); }) ->filterColumn('host', function ($query, $keyword) { $query->whereHas('host', function ($q) use ($keyword) { $q->where('host', 'like', "%{$keyword}%"); }); }) ->rawColumns(['action', 'status']) ->make(true); } }