update datatable

This commit is contained in:
pbao9
2026-01-30 13:56:32 +07:00
parent 9802839056
commit cfe38232c3
10 changed files with 292 additions and 215 deletions
@@ -10,6 +10,7 @@ use App\Repositories\vCenter\vCenterRepositoryInterface;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;
class Datacenter extends Controller
{
@@ -41,7 +42,7 @@ class Datacenter extends Controller
$request->validated();
$vcenter = $this->vCenterRepository->find($request->get('vcenter_id'));
if (! $vcenter) {
if (!$vcenter) {
return back()->withErrors('Invalid request.');
}
$isCreated = $this->datacenterRepository->findBy('datacenter_id', $request->get('datacenter_id'));
@@ -62,7 +63,7 @@ class Datacenter extends Controller
{
$datacenter = $this->datacenterRepository->find($id);
if (! $datacenter) {
if (!$datacenter) {
return back()->withErrors('Invalid request.');
}
$title = 'Edit Datacenter';
@@ -72,7 +73,7 @@ class Datacenter extends Controller
{
$datacenter = $this->datacenterRepository->find($id);
if (! $datacenter) {
if (!$datacenter) {
return back()->withErrors('Invalid request.');
}
$request->validated();
@@ -96,7 +97,7 @@ class Datacenter extends Controller
{
$datacenter = $this->datacenterRepository->find($id);
if (! $datacenter) {
if (!$datacenter) {
return back()->withErrors('Invalid request.');
}
@@ -115,4 +116,24 @@ class Datacenter extends Controller
dispatch($syncDatacenter)->delay(now()->addSeconds(5));
return back()->with('success', 'The process is being established, please wait');
}
public function datatable(Request $request)
{
$query = $this->datacenterRepository->filter(
$request->all(),
['vCenter'],
[],
['*'],
true
);
return DataTables::eloquent($query)
->addColumn(
'action',
fn($d) =>
'<a href="' . route('package.datacenter.edit', $d->id) . '" class="btn btn-primary btn-sm">Edit</a>'
)
->make(true);
}
}
@@ -16,19 +16,32 @@ class DatacenterCachedRepository extends BaseCachedRepository implements Datacen
{
return $this->datacenterRepository->getModel();
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
if (
$returnQuery ||
!empty($data['paginate'])
) {
return $this->datacenterRepository
->filter($data, $relationships, $withCount, $select, $returnQuery);
}
$cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
'filter' => compact('data', 'relationships', 'withCount', 'select')
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->datacenterRepository->filter($data, $relationships, $withCount, $select);
});
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->datacenterRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
public function findBy(string $key = '', string|int|null $value = '')
{
@@ -12,22 +12,39 @@ class DatacenterRepository extends BaseRepository implements DatacenterRepositor
return ModuleDatacenter::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$model = $this->model->with($relationships)->select($select);
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
$query = $this->model
->select($select)
->with($relationships);
if (isset($data['vcenter_id']) && $data['vcenter_id']) {
$model = $model->where("vcenter_id", $data['vcenter_id']);
if (!empty($data['vcenter_id'])) {
$query->where('vcenter_id', $data['vcenter_id']);
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
if (!empty($withCount)) {
$query->withCount($withCount);
}
if (isset($data['page'])) {
return $model->paginate(15);
if ($returnQuery) {
return $query;
}
return $model->get();
if (!empty($data['paginate'])) {
return $query->paginate(
$data['per_page'] ?? 15
);
}
return $query->get();
}
public function findBy(string $key = '', string|int|null $value = '')
{
if (empty($key) || empty($value)) {
@@ -3,7 +3,15 @@ namespace App\Repositories\Module\Datacenter;
use App\Repositories\BaseRepositoryInterface;
interface DatacenterRepositoryInterface extends BaseRepositoryInterface{
public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
public function findBy(string $key = '',string|int|null $value = '' );
interface DatacenterRepositoryInterface extends BaseRepositoryInterface
{
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
);
public function findBy(string $key = '', string|int|null $value = '');
}