Skip to content

Commit

Permalink
Apply #6554 to relation table queries too
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed May 20, 2023
1 parent 554693b commit b72b2d0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Tables\Columns;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use Throwable;

Expand Down Expand Up @@ -69,7 +70,7 @@ public function getImagePath(): ?string
return $record->getFirstMediaUrl($this->getCollection(), $this->getConversion());
}

public function applyEagerLoading(Builder $query): Builder
public function applyEagerLoading(Builder | Relation $query): Builder | Relation
{
if ($this->isHidden()) {
return $query;
Expand Down
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Tables\Columns;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;

class SpatieTagsColumn extends TagsColumn
Expand Down Expand Up @@ -45,7 +46,7 @@ public function getType(): ?string
return $this->type;
}

public function applyEagerLoading(Builder $query): Builder
public function applyEagerLoading(Builder | Relation $query): Builder | Relation
{
if ($this->isHidden()) {
return $query;
Expand Down
Expand Up @@ -11,7 +11,7 @@

trait InteractsWithTableQuery
{
public function applyRelationshipAggregates(Builder $query): Builder
public function applyRelationshipAggregates(Builder | Relation $query): Builder | Relation
{
return $query->when(
filled([$this->getRelationshipToAvg(), $this->getColumnToAvg()]),
Expand All @@ -34,7 +34,7 @@ public function applyRelationshipAggregates(Builder $query): Builder
);
}

public function applyEagerLoading(Builder $query): Builder
public function applyEagerLoading(Builder | Relation $query): Builder | Relation
{
if ($this->isHidden()) {
return $query;
Expand Down
13 changes: 10 additions & 3 deletions packages/tables/src/Concerns/CanSelectRecords.php
Expand Up @@ -89,9 +89,16 @@ public function getSelectedTableRecords(): Collection
$pivotClass = $relationship->getPivotClass();
$pivotKeyName = app($pivotClass)->getKeyName();

return $this->cachedSelectedTableRecords = $this->hydratePivotRelationForTableRecords($this->selectPivotDataInQuery(
$relationship->wherePivotIn($pivotKeyName, $this->selectedTableRecords),
)->get());
$relationship->wherePivotIn($pivotKeyName, $this->selectedTableRecords);

foreach ($this->getCachedTableColumns() as $column) {
$column->applyEagerLoading($relationship);
$column->applyRelationshipAggregates($relationship);
}

return $this->cachedSelectedTableRecords = $this->hydratePivotRelationForTableRecords(
$this->selectPivotDataInQuery($relationship)->get(),
);
}

public function isTableSelectionEnabled(): bool
Expand Down

0 comments on commit b72b2d0

Please sign in to comment.