fix(graphql): fix sort by relation (#967)

This commit is contained in:
Kyrch
2025-09-21 03:00:46 -03:00
committed by GitHub
parent 414836a58f
commit b262fdf08c
3 changed files with 16 additions and 1 deletions
@@ -74,6 +74,20 @@ trait SortsModels
}
if ($sortType === SortType::RELATION) {
if ($relation === null) {
throw new InvalidArgumentException("The 'relation' argument is required for the {$column} column with aggregate sort type.");
}
$builder->withAggregate([
"$relation as {$relation}_$column" => function ($query) use ($column, $direction) {
$query->orderBy($column, $direction);
},
], $column);
$builder->orderBy("{$relation}_$column", $direction);
}
if ($sortType === SortType::COUNT_RELATION) {
if ($relation === null) {
throw new InvalidArgumentException("The 'relation' argument is required for the {$column} column with relation sort type.");
}
+1
View File
@@ -10,4 +10,5 @@ enum SortType: int
case ROOT = 1;
case AGGREGATE = 2;
case RELATION = 3;
case COUNT_RELATION = 4;
}
@@ -34,7 +34,7 @@ class CountField extends Field implements DisplayableField, SortableField
public function sortType(): SortType
{
return SortType::RELATION;
return SortType::COUNT_RELATION;
}
/**