diff --git a/packages/tables/src/Grouping/Concerns/BelongsToTable.php b/packages/tables/src/Grouping/Concerns/BelongsToTable.php new file mode 100644 index 0000000000..af0dcead88 --- /dev/null +++ b/packages/tables/src/Grouping/Concerns/BelongsToTable.php @@ -0,0 +1,28 @@ +table = $table; + + return $this; + } + + public function getTable(): Table + { + return $this->table; + } + + public function getLivewire(): HasTable + { + return $this->getTable()->getLivewire(); + } +} diff --git a/packages/tables/src/Grouping/Group.php b/packages/tables/src/Grouping/Group.php index f701ca8cf9..2c33255f50 100644 --- a/packages/tables/src/Grouping/Group.php +++ b/packages/tables/src/Grouping/Group.php @@ -17,6 +17,8 @@ use Illuminate\Support\Arr; class Group extends Component { + use Concerns\BelongsToTable; + protected ?string $column; protected ?Closure $getDescriptionFromRecordUsing = null; @@ -33,7 +35,7 @@ class Group extends Component protected ?Closure $scopeQueryByKeyUsing = null; - protected string | Closure | null $label; + protected string | Closure | null $label = null; protected string $id; @@ -471,4 +473,16 @@ class Group extends Component return $query->with([$relationshipName]); } + + /** + * @return array + */ + protected function resolveDefaultClosureDependencyForEvaluationByName(string $parameterName): array + { + return match ($parameterName) { + 'livewire' => [$this->getLivewire()], + 'table' => [$this->getTable()], + default => parent::resolveDefaultClosureDependencyForEvaluationByName($parameterName), + }; + } } diff --git a/packages/tables/src/Table/Concerns/CanGroupRecords.php b/packages/tables/src/Table/Concerns/CanGroupRecords.php index 0020a3ab8a..aab0368800 100644 --- a/packages/tables/src/Table/Concerns/CanGroupRecords.php +++ b/packages/tables/src/Table/Concerns/CanGroupRecords.php @@ -72,6 +72,10 @@ trait CanGroupRecords public function defaultGroup(string | Group | null $group): static { + if ($group instanceof Group) { + $group->table($this); + } + $this->defaultGroup = $group; return $this; @@ -82,6 +86,12 @@ trait CanGroupRecords */ public function groups(array | Closure $groups): static { + foreach ($groups as $group) { + if ($group instanceof Group) { + $group->table($this); + } + } + $this->groups = $groups; return $this; @@ -159,7 +169,8 @@ trait CanGroupRecords return $group; } - return Group::make($this->defaultGroup); + return Group::make($this->defaultGroup) + ->table($this); } /** @@ -171,7 +182,8 @@ trait CanGroupRecords $this->evaluate($this->groups), function (array $carry, $group): array { if (! $group instanceof Group) { - $group = Group::make($group); + $group = Group::make($group) + ->table($this); } $carry[$group->getId()] = $group;