networkRepository->filter( [ 'page' => $request->get('page') ?? 1 ], ['host.datacenter.vCenter'] ); $title = 'List all network'; return view('administrator.pages.package.network.index', compact('networks', 'title')); } public function sync(SyncNetwork $syncNetwork) { dispatch($syncNetwork)->delay(now()->addSeconds(5)); return back()->with('success', 'The process is being established, please wait'); } public function create() { $title = 'Add Network'; return view('administrator.pages.package.network.add', compact('title')); } public function store(NetworkRequest $request) { $request->validated(); $host = $this->hostRepository->find($request->get('module_host_id')); if (!$host) { return back()->withErrors('Invalid Request'); } $isCreated = $this->networkRepository->findBy('network', $request->get('network')); if ($isCreated) { return back()->withErrors('The Network ID already exists'); } DB::beginTransaction(); try { $this->networkRepository->create($request->all()); DB::commit(); } catch (Exception $error) { DB::rollBack(); return back()->withErrors($error->getMessage()); } return redirect()->route('package.network.index')->with('success', 'Add new Network successfully'); } public function edit(int $id = 0) { $network = $this->networkRepository->findWithRelationships($id, ['host.datacenter']); if (!$network) { return back()->withErrors('Invalid Request'); } $title = "View Network Information"; return view('administrator.pages.package.network.edit', compact('network', 'title')); } public function update(int $id = 0, NetworkRequest $request) { $request->validated(); $host = $this->networkRepository->find($id); if (!$host) { return redirect()->route('package.network.index')->withErrors('Invalid Request'); } if ($request->get('network') !== $host->network) { $isCreated = $this->networkRepository->findBy('network', $request->get('network')); if ($isCreated) { return back()->withErrors('The Network ID already exists'); } } try { DB::beginTransaction(); $this->networkRepository->update($id, $request->all()); DB::commit(); } catch (Exception $exception) { DB::rollBack(); return back()->withErrors($exception->getMessage()); } return redirect()->route('package.network.index')->with('success', 'Update successfully'); } public function destroy(int $id = 0) { $host = $this->networkRepository->find($id); if (!$host) { return back()->withErrors('Invalid request.'); } DB::beginTransaction(); try { $this->networkRepository->delete($id); DB::commit(); } catch (Exception $error) { DB::rollBack(); return back()->withErrors($error->getMessage()); } return redirect()->route('package.network.index')->with('success', 'Deleted successfully!'); } public function datatable(Request $request) { $query = $this->networkRepository->filter( $request->all(), ['host.datacenter.vCenter'], [], ['*'], true ); return DataTables::eloquent($query) ->addColumn( 'action', fn($d) => 'Edit' ) ->filterColumn('host.datacenter.v_center.host', function ($query, $keyword) { $query->whereHas('host.datacenter.vCenter', function ($q) use ($keyword) { $q->where('host', 'like', "%{$keyword}%"); }); }) ->make(true); } }