From 8c6208471a59ab8896a758fd46f7c566fc4a7e74 Mon Sep 17 00:00:00 2001 From: Khalil Laleh Date: Fri, 27 Nov 2020 20:33:26 +0330 Subject: [PATCH] Modify getRelationCountHash to prevent changing the counter and provide a way to predict the hash --- .../Database/Eloquent/Concerns/QueriesRelationships.php | 4 +++- src/Illuminate/Database/Eloquent/Relations/BelongsTo.php | 5 +++-- src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php | 5 +++-- .../Database/Eloquent/Relations/HasManyThrough.php | 5 +++-- src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php | 5 +++-- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php index 065e1b4fbcd7..c171b4e07813 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php @@ -383,8 +383,10 @@ public function withAggregate($relations, $column, $function = null) $relation = $this->getRelationWithoutConstraints($name); if ($function) { + $predictedColumn = $this->getQuery()->from === $relation->getQuery()->getQuery()->from ? "{$relation->getRelationCountHash(true)}.$column" : $column; + $expression = sprintf('%s(%s)', $function, $this->getQuery()->getGrammar()->wrap( - $column === '*' ? $column : $relation->getRelated()->qualifyColumn($column) + $column === '*' ? $column : $relation->getRelated()->qualifyColumn($predictedColumn) )); } else { $expression = $column; diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index 664f2b8e8049..06ee6d5c29c2 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -282,11 +282,12 @@ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder /** * Get a relationship join table hash. * + * @param bool $fixed * @return string */ - public function getRelationCountHash() + public function getRelationCountHash($fixed = false) { - return 'laravel_reserved_'.static::$selfJoinCount++; + return 'laravel_reserved_'.($fixed ? static::$selfJoinCount : static::$selfJoinCount++); } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index b54340ec2559..d8c14ef33961 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -1170,11 +1170,12 @@ public function getExistenceCompareKey() /** * Get a relationship join table hash. * + * @param bool $fixed * @return string */ - public function getRelationCountHash() + public function getRelationCountHash($fixed = false) { - return 'laravel_reserved_'.static::$selfJoinCount++; + return 'laravel_reserved_'.($fixed ? static::$selfJoinCount : static::$selfJoinCount++); } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index b0b568b25f01..15f8dabf0cbf 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -599,11 +599,12 @@ public function getRelationExistenceQueryForThroughSelfRelation(Builder $query, /** * Get a relationship join table hash. * + * @param bool $fixed * @return string */ - public function getRelationCountHash() + public function getRelationCountHash($fixed = false) { - return 'laravel_reserved_'.static::$selfJoinCount++; + return 'laravel_reserved_'.($fixed ? static::$selfJoinCount : static::$selfJoinCount++); } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index acff8314640e..b9a70b964da0 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -366,11 +366,12 @@ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder /** * Get a relationship join table hash. * + * @param bool $fixed * @return string */ - public function getRelationCountHash() + public function getRelationCountHash($fixed = false) { - return 'laravel_reserved_'.static::$selfJoinCount++; + return 'laravel_reserved_'.($fixed ? static::$selfJoinCount : static::$selfJoinCount++); } /**