diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index 9df11e64002f..1b1f4a5dea72 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -30,6 +30,13 @@ class BelongsTo extends Relation */ protected $relation; + /** + * The count of self joins. + * + * @var int + */ + protected static $selfJoinCount = 0; + /** * Create a new belongs to relationship instance. * @@ -125,7 +132,7 @@ public function getRelationQueryForSelfRelation(Builder $query, Builder $parent, */ public function getRelationCountHash() { - return 'self_'.md5(microtime(true)); + return 'laravel_reserved_'.static::$selfJoinCount++; } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 5cbab4a51b12..4fd317ef8f94 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -67,6 +67,13 @@ class BelongsToMany extends Relation */ protected $pivotUpdatedAt; + /** + * The count of self joins. + * + * @var int + */ + protected static $selfJoinCount = 0; + /** * Create a new belongs to many relationship instance. * @@ -376,7 +383,7 @@ public function getRelationQueryForSelfJoin(Builder $query, Builder $parent, $co */ public function getRelationCountHash() { - return 'self_'.md5(microtime(true)); + return 'laravel_reserved_'.static::$selfJoinCount++; } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 7b33c02a6a9e..c54c4da053c0 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -23,6 +23,13 @@ abstract class HasOneOrMany extends Relation */ protected $localKey; + /** + * The count of self joins. + * + * @var int + */ + protected static $selfJoinCount = 0; + /** * Create a new has one or many relationship instance. * @@ -99,7 +106,7 @@ public function getRelationQueryForSelfRelation(Builder $query, Builder $parent, */ public function getRelationCountHash() { - return 'self_'.md5(microtime(true)); + return 'laravel_reserved_'.static::$selfJoinCount++; } /** diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 41e0499f6760..30d37cb84aa0 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -553,7 +553,7 @@ public function testSelfHasNested() // alias has a dynamic hash, so replace with a static string for comparison $alias = 'self_alias_hash'; - $aliasRegex = '/\b(self_[a-f0-9]{32})(\b|$)/i'; + $aliasRegex = '/\b(laravel_reserved_\d)(\b|$)/i'; $nestedSql = preg_replace($aliasRegex, $alias, $nestedSql); $dotSql = preg_replace($aliasRegex, $alias, $dotSql); @@ -569,7 +569,7 @@ public function testSelfHasNestedUsesAlias() // alias has a dynamic hash, so replace with a static string for comparison $alias = 'self_alias_hash'; - $aliasRegex = '/\b(self_[a-f0-9]{32})(\b|$)/i'; + $aliasRegex = '/\b(laravel_reserved_\d)(\b|$)/i'; $sql = preg_replace($aliasRegex, $alias, $sql);