field->{'relation'}(); } catch (Exception) { throw new Exception("'relation' method is required for the aggregate sort type."); } $builder->withAggregate([ "$relation as {$relation}_value" => function ($query): void { $query->orderBy('value', $this->direction->value); }, ], 'value'); return $builder->orderBy("{$relation}_value", $this->direction->value); } }