add group by feature to datatable
This commit is contained in:
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user