Skip to content

Commit 1996e69

Browse files
cbltaylorotwell
andauthored
[8.x] Add default "_of_many" to join alias when relation name is table name (#37411)
* improved default alias * Update CanBeOneOfMany.php Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent b481c3a commit 1996e69

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public function ofMany($column = 'id', $aggregate = 'MAX', $relation = null)
6464
{
6565
$this->isOneOfMany = true;
6666

67-
$this->relationName = $relation ?: $this->guessRelationship();
67+
$this->relationName = $relation ?: $this->getDefaultOneOfManyJoinAlias(
68+
$this->guessRelationship()
69+
);
6870

6971
$keyName = $this->query->getModel()->getKeyName();
7072

@@ -110,6 +112,19 @@ public function ofMany($column = 'id', $aggregate = 'MAX', $relation = null)
110112
return $this;
111113
}
112114

115+
/**
116+
* Get the default alias for one of many inner join clause.
117+
*
118+
* @param string $relation
119+
* @return string
120+
*/
121+
protected function getDefaultOneOfManyJoinAlias($relation)
122+
{
123+
return $relation == $this->query->getModel()->getTable()
124+
? $relation.'_of_many'
125+
: $relation;
126+
}
127+
113128
/**
114129
* Indicate that the relation is the latest single result of a larger one-to-many relationship.
115130
*

tests/Database/DatabaseEloquentHasOneOfManyTest.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,32 @@ protected function tearDown(): void
7070

7171
public function testItGuessesRelationName()
7272
{
73-
$user = HasOneOfManyTestUser::create();
73+
$user = HasOneOfManyTestUser::make();
7474
$this->assertSame('latest_login', $user->latest_login()->getRelationName());
7575
}
7676

77-
// public function testRelationNameCanBeSet()
78-
// {
79-
// $user = HasOneOfManyTestUser::create();
80-
// $this->assertSame('foo', $user->latest_login_with_other_name()->getRelationName());
81-
// }
77+
public function testItGuessesRelationNameAndAddsOfManyWhenTableNameIsRelationName()
78+
{
79+
$model = HasOneOfManyTestModel::make();
80+
$this->assertSame('logins_of_many', $model->logins()->getRelationName());
81+
}
82+
83+
public function testRelationNameCanBeSet()
84+
{
85+
$user = HasOneOfManyTestUser::create();
86+
87+
// Using "ofMany"
88+
$relation = $user->latest_login()->ofMany('id', 'max', 'foo');
89+
$this->assertSame('foo', $relation->getRelationName());
90+
91+
// Using "latestOfMAny"
92+
$relation = $user->latest_login()->latestOfMAny('id', 'bar');
93+
$this->assertSame('bar', $relation->getRelationName());
94+
95+
// Using "oldestOfMAny"
96+
$relation = $user->latest_login()->oldestOfMAny('id', 'baz');
97+
$this->assertSame('baz', $relation->getRelationName());
98+
}
8299

83100
public function testQualifyingSubSelectColumn()
84101
{
@@ -225,9 +242,6 @@ public function testIsNotMethod()
225242
$this->assertFalse($user->latest_login()->isNot($login2));
226243
}
227244

228-
/**
229-
* @group fail
230-
*/
231245
public function testGet()
232246
{
233247
$user = HasOneOfManyTestUser::create();
@@ -392,6 +406,14 @@ public function price_with_shortcut()
392406
}
393407
}
394408

409+
class HasOneOfManyTestModel extends Eloquent
410+
{
411+
public function logins()
412+
{
413+
return $this->hasOne(HasOneOfManyTestLogin::class)->ofMany();
414+
}
415+
}
416+
395417
class HasOneOfManyTestLogin extends Eloquent
396418
{
397419
protected $table = 'logins';

0 commit comments

Comments
 (0)