hostRepository->filter(
$request->all(),
['datacenter.vCenter'],
[],
['*'],
true
);
return DataTables::eloquent($query)
->editColumn('updated_at', function ($d) {
return $d->updated_at ? $d->updated_at->format('d/m/Y') : '';
})
->addColumn('action', function ($d) {
$editUrl = route('package.host.edit', ['id' => $d->id]);
$deleteBtn = '';
if (auth()->guard('admins')->user()->can("delete module vcenter")) {
$deleteUrl = route('package.host.destroy', ['id' => $d->id]);
$csrf = csrf_field();
$method = method_field('delete');
$deleteBtn = <<
$method
$csrf
HTML;
}
return <<
$deleteBtn
HTML;
})
->filterColumn('datacenter.name', function ($query, $keyword) {
$query->whereHas('datacenter', function ($q) use ($keyword) {
$q->where('name', 'like', "%{$keyword}%");
});
})
->filterColumn('datacenter.v_center.host', function ($query, $keyword) {
$query->whereHas('datacenter.vCenter', function ($q) use ($keyword) {
$q->where('host', 'like', "%{$keyword}%");
});
})
->rawColumns(['action'])
->make(true);
}
public function sync(SyncHost $syncHost)
{
dispatch($syncHost)->delay(now()->addSeconds(5));
return back()->with('success', 'The process is being established, please wait');
}
public function create()
{
$title = 'Add host';
return view('admin.package.host.add', compact('title'));
}
public function store(HostRequest $request)
{
$request->validated();
$datacenter = $this->datacenterRepository->find($request->get('module_datacenter_id'));
if (!$datacenter) {
return back()->withErrors('Invalid Request');
}
$isCreated = $this->hostRepository->findBy('host_id', $request->get('host_id'));
if ($isCreated) {
return back()->withErrors('The Host ID already exists');
}
DB::beginTransaction();
try {
$this->hostRepository->create($request->all());
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('package.host.index')->with('success', 'Add new host successfully');
}
public function edit(int $id = 0)
{
$host = $this->hostRepository->findWithRelationships($id, ['datacenter']);
if (!$host) {
return back()->withErrors('Invalid Request');
}
$title = "View Host Information";
return view('admin.package.host.edit', compact('host', 'title'));
}
public function update(int $id = 0, HostRequest $request)
{
$request->validated();
$host = $this->hostRepository->find($id);
if (!$host) {
return redirect()->route('package.host.index')->withErrors('Invalid Request');
}
if ($request->get('host_id') !== $host->host_id) {
$isCreated = $this->hostRepository->findBy('host_id', $request->get('host_id'));
if ($isCreated) {
return back()->withErrors('The Host ID already exists');
}
}
try {
DB::beginTransaction();
$this->hostRepository->update($id, $request->all());
DB::commit();
} catch (Exception $exception) {
DB::rollBack();
return back()->withErrors($exception->getMessage());
}
return redirect()->route('package.host.index')->with('success', 'Update successfully');
}
public function destroy(int $id = 0)
{
$host = $this->hostRepository->find($id);
if (!$host) {
return back()->withErrors('Invalid request.');
}
DB::beginTransaction();
try {
$this->hostRepository->delete($id);
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('package.host.index')->with('success', 'Deleted successfully!');
}
}