Skip to content

Commit 571f09c

Browse files
authored
fix: allow non nested joins to specify pivot information (#173)
1 parent 01e70ed commit 571f09c

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/Mixins/JoinRelationship.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,19 @@ public function joinRelationship(): Closure
115115
return $this;
116116
}
117117

118+
$relationCallback = $callback;
119+
if ($callback && is_array($callback) && isset($callback[$relationName]) && is_array($callback[$relationName])) {
120+
$relationCallback = $callback[$relationName];
121+
}
122+
118123
$relation = $this->getModel()->{$relationName}();
119124
$relationQuery = $relation->getQuery();
120125
$alias = $joinHelper->getAliasName(
121126
$useAlias,
122127
$relation,
123128
$relationName,
124129
$relationQuery->getModel()->getTable(),
125-
$callback
130+
$relationCallback
126131
);
127132

128133
if ($relation instanceof BelongsToMany && !is_array($alias)) {
@@ -131,12 +136,13 @@ public function joinRelationship(): Closure
131136
$relation,
132137
$relationName,
133138
$relation->getTable(),
134-
$callback
139+
$relationCallback
135140
);
136141
$alias = [$extraAlias, $alias];
137142
}
138143

139144
$aliasString = is_array($alias) ? implode('.', $alias) : $alias;
145+
$useAlias = $alias ? true : $useAlias;
140146

141147
$relationJoinCache = $alias
142148
? "{$aliasString}.{$relationQuery->getModel()->getTable()}.{$relationName}"
@@ -146,21 +152,23 @@ public function joinRelationship(): Closure
146152
return $this;
147153
}
148154

155+
if ($useAlias) {
156+
StaticCache::setTableAliasForModel($relation->getModel(), $alias);
157+
}
149158

150159
$joinHelper->markRelationshipAsAlreadyJoined($this->getModel(), $relationJoinCache);
151160
StaticCache::clear();
152161

153162
$relation->performJoinForEloquentPowerJoins(
154163
builder: $this,
155164
joinType: $joinType,
156-
callback: $callback,
165+
callback: $relationCallback,
157166
alias: $alias,
158167
disableExtraConditions: $disableExtraConditions,
159168
morphable: $morphable,
160169
);
161170

162171
return $this;
163-
164172
};
165173
}
166174

@@ -353,7 +361,8 @@ public function orderByPowerJoins(): Closure
353361
}, $this->getModel());
354362

355363
if ($aggregation) {
356-
$aliasName = sprintf('%s_%s_%s',
364+
$aliasName = sprintf(
365+
'%s_%s_%s',
357366
$latestRelationshipModel->getTable(),
358367
$column,
359368
$aggregation
@@ -382,7 +391,6 @@ public function orderByPowerJoins(): Closure
382391
}
383392
return $this;
384393
};
385-
386394
}
387395

388396
public function orderByLeftPowerJoins(): Closure
@@ -517,7 +525,7 @@ public function hasNestedUsingJoins(): Closure
517525
foreach ($relations as $index => $relation) {
518526
$relationName = $relation;
519527

520-
if (! $latestRelation) {
528+
if (!$latestRelation) {
521529
$relation = $this->getRelationWithoutConstraintsProxy($relation);
522530
} else {
523531
$relation = $latestRelation->getModel()->query()->getRelationWithoutConstraintsProxy($relation);
@@ -540,7 +548,6 @@ public function powerJoinDoesntHave(): Closure
540548
return function ($relation, $boolean = 'and', Closure $callback = null) {
541549
return $this->powerJoinHas($relation, '<', 1, $boolean, $callback);
542550
};
543-
544551
}
545552

546553
public function powerJoinWhereHas(): Closure

tests/JoinRelationshipTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,8 @@ public function test_join_has_many_through_relationship_with_alias()
537537
{
538538
$query = User::joinRelationship('commentsThroughPosts.user', [
539539
'commentsThroughPosts' => [
540-
'posts' => fn($join) => $join->as('posts_alias'),
541-
'comments' => fn($join) => $join->as('comments_alias'),
540+
'posts' => fn ($join) => $join->as('posts_alias'),
541+
'comments' => fn ($join) => $join->as('comments_alias'),
542542
],
543543
])->toSql();
544544

@@ -558,8 +558,8 @@ public function test_join_belongs_to_many_relationship_with_alias()
558558
{
559559
$query = Group::joinRelationship('posts.user', [
560560
'posts' => [
561-
'posts' => fn($join) => $join->as('posts_alias'),
562-
'post_groups' => fn($join) => $join->as('post_groups_alias'),
561+
'posts' => fn ($join) => $join->as('posts_alias'),
562+
'post_groups' => fn ($join) => $join->as('post_groups_alias'),
563563
],
564564
])->toSql();
565565

@@ -574,6 +574,27 @@ public function test_join_belongs_to_many_relationship_with_alias()
574574
);
575575
}
576576

577+
/** @test */
578+
public function test_join_belongs_to_many_with_alias()
579+
{
580+
$query = Group::joinRelationship('posts', [
581+
'posts' => [
582+
'posts' => fn ($join) => $join->as('posts_alias'),
583+
'post_groups' => fn ($join) => $join->as('post_groups_not_nested'),
584+
],
585+
])->toSql();
586+
587+
$this->assertStringContainsString(
588+
'inner join "posts" as "posts_alias"',
589+
$query
590+
);
591+
592+
$this->assertStringContainsString(
593+
'inner join "post_groups" as "post_groups_not_nested"',
594+
$query
595+
);
596+
}
597+
577598
/** @test */
578599
public function test_it_joins_different_tables_with_same_relationship_name()
579600
{

0 commit comments

Comments
 (0)