From 7ec74a74d656fcce0d4c8a2308dc49541d485359 Mon Sep 17 00:00:00 2001 From: Connor Abbas Date: Sat, 17 May 2025 15:03:46 +0000 Subject: [PATCH 1/2] Filterable Trait Fixes --- app/Models/Traits/Filterable.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Models/Traits/Filterable.php b/app/Models/Traits/Filterable.php index aa47b53c..93e57b28 100644 --- a/app/Models/Traits/Filterable.php +++ b/app/Models/Traits/Filterable.php @@ -20,10 +20,10 @@ abstract protected function getFilterableColumns(): array; * Validate if the column is filterable/sortable. * @throws InvalidArgumentException */ - protected function validateColumn(string $column): void + public function validateColumn(string $column): void { if (!in_array($column, $this->getFilterableColumns(), true)) { - throw new InvalidArgumentException("Invalid column name: {$column}."); + throw new InvalidArgumentException("Invalid filterable/sortable column name: {$column}."); } } @@ -57,7 +57,7 @@ public function scopeApplyRelationFilter( $related = $relationQuery->getModel(); $relatedClass = get_class($related); $usedTraits = class_uses_recursive($relatedClass); - $thisTrait = self::class; + $thisTrait = Filterable::class; if (!in_array($thisTrait, $usedTraits, true)) { throw new InvalidArgumentException("Related model {$relatedClass} must use the {$thisTrait} trait."); From f80a13c83b2159f04fca0e1f896db8d2c8419655 Mon Sep 17 00:00:00 2001 From: Connor Abbas Date: Sat, 17 May 2025 15:56:45 +0000 Subject: [PATCH 2/2] another fix --- app/Models/Traits/Filterable.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Models/Traits/Filterable.php b/app/Models/Traits/Filterable.php index 93e57b28..b2dd7d79 100644 --- a/app/Models/Traits/Filterable.php +++ b/app/Models/Traits/Filterable.php @@ -55,6 +55,7 @@ public function scopeApplyRelationFilter( ): void { $query->whereHas($relation, function (Builder $relationQuery) use ($column, $matchMode, $value) { $related = $relationQuery->getModel(); + $fullQualifiedColumnName = $related->getTable() . ".$column"; $relatedClass = get_class($related); $usedTraits = class_uses_recursive($relatedClass); $thisTrait = Filterable::class; @@ -66,7 +67,7 @@ public function scopeApplyRelationFilter( /** @phpstan-ignore-next-line */ $related->validateColumn($column); /** @phpstan-ignore-next-line */ - $this->applyFilterLogic($relationQuery, $column, $matchMode, $value); + $this->applyFilterLogic($relationQuery, $fullQualifiedColumnName, $matchMode, $value); }); }