Skip to content

Commit ed938b3

Browse files
committed
Setting alias correctly when using ->as() in nested joins
1 parent 9736413 commit ed938b3

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
"require-dev": {
2626
"laravel/legacy-factories": "^1.0@dev",
2727
"orchestra/testbench": "4.*|5.*|6.*",
28-
"phpunit/phpunit": "^8.0",
29-
"spatie/laravel-ray": "dev-master"
28+
"phpunit/phpunit": "^8.0"
3029
},
3130
"autoload": {
3231
"psr-4": {

src/Mixins/RelationshipsExtraMethods.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()
8181
[$alias1, $alias2] = $alias;
8282

8383
$joinedTable = $alias1 ?: $this->getTable();
84-
$parentTable = $this->getTableOrAliasForModel($this->parent)[1] ?? $this->parent->getTable();
84+
$parentTable = $this->getTableOrAliasForModel($this->parent) ?? $this->parent->getTable();
8585

8686
$builder->{$joinType}($this->getTable(), function ($join) use ($callback, $joinedTable, $parentTable, $alias1) {
8787
if ($alias1) {

src/PowerJoinClause.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public function as(string $alias)
5858
$this->table = sprintf('%s as %s', $this->table, $alias);
5959
$this->useTableAliasInConditions();
6060

61+
if ($this->model) {
62+
PowerJoins::$powerJoinAliasesCache[spl_object_id($this->model)] = $alias;
63+
}
64+
6165
return $this;
6266
}
6367

tests/JoinRelationshipUsingAliasTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public function test_alias_for_belongs_to_many_nested()
130130
$user1->groups()->attach($group);
131131
$group->posts()->attach($post);
132132

133-
$users = User::query()->joinRelationshipUsingAlias('groups.posts')->get();
134133
$query = User::query()->joinRelationshipUsingAlias('groups.posts')->toSql();
134+
$users = User::query()->joinRelationshipUsingAlias('groups.posts')->get();
135135

136136
$this->assertCount(1, $users);
137137
$this->assertEquals($user1->id, $users->first()->id);

0 commit comments

Comments
 (0)