Skip to content

Commit a307fab

Browse files
marickvantuilMarick van Tuil
and
Marick van Tuil
authored
Fix alias not fully applying in has one/many through (#205)
* Fix alias not fully applying in has one/many through * Fix tes --------- Co-authored-by: Marick van Tuil <[email protected]>
1 parent 84a2478 commit a307fab

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/JoinsHelper.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Builder;
77
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
88
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
9+
use Illuminate\Database\Eloquent\Relations\HasOneOrManyThrough;
910
use Illuminate\Database\Eloquent\Relations\Relation;
1011
use WeakMap;
1112

@@ -172,6 +173,32 @@ public function getAliasName(bool $useAlias, Relation $relation, string $relatio
172173
}
173174
}
174175

176+
if (is_array($callback) && $relation instanceof HasOneOrManyThrough) {
177+
$alias = [null, null];
178+
179+
$throughParentTable = $relation->getThroughParent()->getTable();
180+
if (isset($callback[$throughParentTable])) {
181+
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $throughParentTable);
182+
$callback[$throughParentTable]($fakeJoinCallback);
183+
184+
if ($fakeJoinCallback->getAlias()) {
185+
$alias[0] = $fakeJoinCallback->getAlias();
186+
}
187+
}
188+
189+
$farParentTable = $relation->getFarParent()->getTable();
190+
if (isset($callback[$farParentTable])) {
191+
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $farParentTable);
192+
$callback[$farParentTable]($fakeJoinCallback);
193+
194+
if ($fakeJoinCallback->getAlias()) {
195+
$alias[1] = $fakeJoinCallback->getAlias();
196+
}
197+
}
198+
199+
return $alias;
200+
}
201+
175202
if (is_array($callback) && isset($callback[$tableName])) {
176203
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $tableName);
177204
$callback[$tableName]($fakeJoinCallback);

tests/JoinRelationshipUsingAliasTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,53 @@ public function test_join_model_with_soft_deletes_using_alias()
286286
$this->assertQueryContains($expected, $queryB);
287287
$this->assertQueryContains($expected, $queryC);
288288
}
289+
290+
/** @test */
291+
public function test_join_through_model_with_soft_deletes_using_alias()
292+
{
293+
// has one through
294+
$query = Comment::query()->joinRelationship('postCategory', [
295+
'postCategory' => [
296+
'posts' => fn ($join) => $join->as('posts_alias'),
297+
],
298+
])->toSql();
299+
300+
$this->assertQueryContains(
301+
$expected = 'select comments.* from comments inner join posts as posts_alias on posts_alias.id = comments.post_id and posts_alias.deleted_at is null inner join categories on categories.id = posts_alias.category_id',
302+
$query
303+
);
304+
305+
// has many through
306+
$query = User::query()->joinRelationship('commentsThroughPosts', [
307+
'comments' => fn ($join) => $join->as('comments_alias'),
308+
'posts' => fn ($join) => $join->as('posts_alias'),
309+
])->toSql();
310+
311+
$this->assertQueryContains(
312+
$expected = 'select "users".* from "users" inner join "posts" as "posts_alias" on "posts_alias"."user_id" = "users"."id" and "posts_alias"."deleted_at" is null inner join "comments" as "comments_alias" on "comments_alias"."post_id" = "posts_alias"."id" where "users"."deleted_at" is null',
313+
$query
314+
);
315+
316+
// ensure for nested relation too
317+
$query = Post::query()->joinRelationship('lastComment.postCategory', [
318+
'postCategory' => [
319+
'posts' => fn ($join) => $join->as('posts_alias'),
320+
],
321+
])->toSql();
322+
323+
$this->assertQueryContains(
324+
$expected = 'select "posts".* from "posts"',
325+
$query
326+
);
327+
328+
$this->assertQueryContains(
329+
$expected = 'inner join "posts" as "posts_alias" on "posts_alias"."id" = "comments"."post_id" and "posts_alias"."deleted_at"',
330+
$query
331+
);
332+
333+
$this->assertQueryContains(
334+
$expected = 'inner join "categories" on "categories"."id" = "posts_alias"."category_id"',
335+
$query
336+
);
337+
}
289338
}

0 commit comments

Comments
 (0)