add group by feature to datatable
This commit is contained in:
@@ -22,6 +22,7 @@ class DataTableInput
|
||||
private array $rels,
|
||||
private array $allowedFilters,
|
||||
private array $allowedSortings,
|
||||
private ?array $GroupBy,
|
||||
)
|
||||
{
|
||||
}
|
||||
@@ -74,4 +75,9 @@ class DataTableInput
|
||||
{
|
||||
return $this->rels;
|
||||
}
|
||||
|
||||
public function getGroupBy(): ?array
|
||||
{
|
||||
return $this->GroupBy;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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