Skip to content

Commit d118dbd

Browse files
committed
Fixed usage of alias coming from the 'from' method
1 parent 80e7f13 commit d118dbd

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/JoinsHelper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,18 @@ public static function ensureModelIsUniqueToQuery($query): void
7777
// If the model is already associated with another query, we need to clone the model.
7878
// This can happen if a certain query, *before having interacted with the library
7979
// `joinRelationship()` method*, was cloned by previous code.
80+
81+
// Preserve the from clause (including any alias) before setModel overwrites it
82+
$originalFrom = $query->getQuery()->from;
83+
8084
$query->setModel($model = new ($query->getModel()));
8185
$model->mergeCasts($originalModel->getCasts());
8286

87+
// Restore the original from clause if it was set
88+
if ($originalFrom) {
89+
$query->getQuery()->from = $originalFrom;
90+
}
91+
8392
// Link the Spl Object ID of the query to the new model...
8493
static::$modelQueryDictionary[$model] = $querySplObjectId;
8594

@@ -99,10 +108,18 @@ public static function ensureModelIsUniqueToQuery($query): void
99108
$originalModel = $query->getModel();
100109
$originalJoinsHelper = JoinsHelper::make($originalModel);
101110

111+
// Preserve the from clause (including any alias) before setModel overwrites it
112+
$originalFrom = $query->getQuery()->from;
113+
102114
// Ensure the model of the cloned query is unique to the query.
103115
$query->setModel($model = new $originalModel());
104116
$model->mergeCasts($originalModel->getCasts());
105117

118+
// Restore the original from clause if it was set
119+
if ($originalFrom) {
120+
$query->getQuery()->from = $originalFrom;
121+
}
122+
106123
// Update any `beforeQueryCallbacks` to link to the new `$this` as Eloquent Query,
107124
// otherwise the reference to the current Eloquent query goes wrong. These query
108125
// callbacks are stored on the `QueryBuilder` instance and therefore do not get

src/Mixins/JoinRelationship.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,17 @@ public function joinRelationship(): Closure
107107
JoinsHelper::ensureModelIsUniqueToQuery($this);
108108
JoinsHelper::clearCacheBeforeQuery($this);
109109

110+
// Check if the main table has an alias (e.g., "posts as p")
111+
$fromClause = $this->getQuery()->from;
112+
$mainTableOrAlias = $this->getModel()->getTable();
113+
if ($fromClause && preg_match('/^.+\s+as\s+["\'\`]?(.+?)["\'\`]?$/i', $fromClause, $matches)) {
114+
// Register the alias for the main model so joins use it
115+
$mainTableOrAlias = $matches[1];
116+
StaticCache::setTableAliasForModel($this->getModel(), $mainTableOrAlias);
117+
}
118+
110119
if (is_null($this->getSelect())) {
111-
$this->select(sprintf('%s.*', $this->getModel()->getTable()));
120+
$this->select(sprintf('%s.*', $mainTableOrAlias));
112121
}
113122

114123
if (Str::contains($relationName, '.')) {
@@ -159,7 +168,6 @@ public function joinRelationship(): Closure
159168
}
160169

161170
$joinHelper->markRelationshipAsAlreadyJoined($this->getModel(), $relationJoinCache);
162-
StaticCache::clear();
163171

164172
$relation->performJoinForEloquentPowerJoins(
165173
builder: $this,
@@ -170,6 +178,11 @@ public function joinRelationship(): Closure
170178
morphable: $morphable,
171179
);
172180

181+
// Clear only the related model's alias from cache after join is performed
182+
if ($useAlias) {
183+
unset(StaticCache::$powerJoinAliasesCache[spl_object_id($relation->getModel())]);
184+
}
185+
173186
return $this;
174187
};
175188
}

tests/JoinRelationshipTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ public function test_join_relationship_preserves_main_table_alias()
10161016

10171017
// The join should reference the alias
10181018
$this->assertQueryContains(
1019-
'inner join "users" on "users"."id" = "p"."user_id"',
1019+
'inner join "users" on "p"."user_id" = "users"."id"',
10201020
$query
10211021
);
10221022
}
@@ -1042,7 +1042,7 @@ public function test_join_relationship_preserves_main_table_alias_with_nested_re
10421042

10431043
// The second join should work normally
10441044
$this->assertQueryContains(
1045-
'inner join "users" on "users"."id" = "comments"."user_id"',
1045+
'inner join "users" on "comments"."user_id" = "users"."id"',
10461046
$query
10471047
);
10481048
}
@@ -1064,7 +1064,7 @@ public function test_join_relationship_preserves_main_table_alias_when_selecting
10641064

10651065
// The join should reference the alias
10661066
$this->assertQueryContains(
1067-
'inner join "users" on "users"."id" = "p"."user_id"',
1067+
'inner join "users" on "p"."user_id" = "users"."id"',
10681068
$query
10691069
);
10701070

0 commit comments

Comments
 (0)