add group by feature to datatable

This commit is contained in:
2025-04-15 10:45:47 +03:30
parent 6f896f4894
commit 8d138b8339
6 changed files with 91 additions and 129 deletions

View File

@@ -13,6 +13,7 @@ class DataTableService
protected array $allowedRelations;
protected array $allowedSortings;
protected array $allowedSelects;
protected array $allowedGroupBy;
private int $totalRowCount;
public function __construct(
@@ -46,6 +47,12 @@ class DataTableService
return $this;
}
public function setAllowedGroupBy(array $allowedGroupBy): DataTableService
{
$this->allowedGroupBy = $allowedGroupBy;
return $this;
}
/**
* Handle 'getData' operations
* @return array
@@ -88,7 +95,9 @@ class DataTableService
foreach ($sorting as $sort){
$query = (new ApplySort($query, $sort))->apply();
}
$query = $this->applyGroupBy($query, $this->allowedGroupBy);
return $query;
}
@@ -101,6 +110,11 @@ class DataTableService
return $query;
}
protected function applyGroupBy(Builder $query, array $groupBy): Builder
{
return empty($groupBy) ? $query : $query->groupBy($groupBy);
}
protected function includeRelationsInQuery(Builder $query, array $rels): Builder
{
if (!empty($rels)) {