diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php index ea1afb539c3..f02311a77b2 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php @@ -64,7 +64,9 @@ public function ofMany($column = 'id', $aggregate = 'MAX', $relation = null) { $this->isOneOfMany = true; - $this->relationName = $relation ?: $this->guessRelationship(); + $this->relationName = $relation ?: $this->getDefaultOneOfManyJoinAlias( + $this->guessRelationship() + ); $keyName = $this->query->getModel()->getKeyName(); @@ -110,6 +112,19 @@ public function ofMany($column = 'id', $aggregate = 'MAX', $relation = null) return $this; } + /** + * Get the default alias for one of many inner join clause. + * + * @param string $relation + * @return string + */ + protected function getDefaultOneOfManyJoinAlias($relation) + { + return $relation == $this->query->getModel()->getTable() + ? $relation.'_of_many' + : $relation; + } + /** * Indicate that the relation is the latest single result of a larger one-to-many relationship. * diff --git a/tests/Database/DatabaseEloquentHasOneOfManyTest.php b/tests/Database/DatabaseEloquentHasOneOfManyTest.php index 37d5925f382..f5d45dfd62b 100755 --- a/tests/Database/DatabaseEloquentHasOneOfManyTest.php +++ b/tests/Database/DatabaseEloquentHasOneOfManyTest.php @@ -70,15 +70,32 @@ protected function tearDown(): void public function testItGuessesRelationName() { - $user = HasOneOfManyTestUser::create(); + $user = HasOneOfManyTestUser::make(); $this->assertSame('latest_login', $user->latest_login()->getRelationName()); } - // public function testRelationNameCanBeSet() - // { - // $user = HasOneOfManyTestUser::create(); - // $this->assertSame('foo', $user->latest_login_with_other_name()->getRelationName()); - // } + public function testItGuessesRelationNameAndAddsOfManyWhenTableNameIsRelationName() + { + $model = HasOneOfManyTestModel::make(); + $this->assertSame('logins_of_many', $model->logins()->getRelationName()); + } + + public function testRelationNameCanBeSet() + { + $user = HasOneOfManyTestUser::create(); + + // Using "ofMany" + $relation = $user->latest_login()->ofMany('id', 'max', 'foo'); + $this->assertSame('foo', $relation->getRelationName()); + + // Using "latestOfMAny" + $relation = $user->latest_login()->latestOfMAny('id', 'bar'); + $this->assertSame('bar', $relation->getRelationName()); + + // Using "oldestOfMAny" + $relation = $user->latest_login()->oldestOfMAny('id', 'baz'); + $this->assertSame('baz', $relation->getRelationName()); + } public function testQualifyingSubSelectColumn() { @@ -225,9 +242,6 @@ public function testIsNotMethod() $this->assertFalse($user->latest_login()->isNot($login2)); } - /** - * @group fail - */ public function testGet() { $user = HasOneOfManyTestUser::create(); @@ -392,6 +406,14 @@ public function price_with_shortcut() } } +class HasOneOfManyTestModel extends Eloquent +{ + public function logins() + { + return $this->hasOne(HasOneOfManyTestLogin::class)->ofMany(); + } +} + class HasOneOfManyTestLogin extends Eloquent { protected $table = 'logins';