Skip to content

Commit f5e9438

Browse files
authored
Merge pull request #2051 from Morinohtar/9.0
[9.0] Sorting nested relation that refers to the same table.
2 parents 16f77dc + b953eac commit f5e9438

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/EloquentDataTable.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
140140
$table = '';
141141
$deletedAt = false;
142142
$lastQuery = $this->query;
143-
foreach (explode('.', $relation) as $eachRelation) {
143+
foreach (explode('.', $relation) as $index => $eachRelation) {
144144
$model = $lastQuery->getRelation($eachRelation);
145145
switch (true) {
146146
case $model instanceof BelongsToMany:
@@ -169,19 +169,24 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
169169

170170
case $model instanceof BelongsTo:
171171
$table = $model->getRelated()->getTable();
172+
$alias = "alias_{$index}_{$table}";
173+
$tableAs = "{$table} AS {$alias}";
172174
$foreign = $model->getQualifiedForeignKeyName();
173-
$other = $model->getQualifiedOwnerKeyName();
175+
$owner = "{$alias}.{$model->getOwnerKeyName()}";
174176
$deletedAt = $this->checkSoftDeletesOnModel($model->getRelated());
177+
175178
break;
176179

177180
default:
178181
throw new Exception('Relation ' . get_class($model) . ' is not yet supported.');
179182
}
180-
$this->performJoin($table, $foreign, $other, $deletedAt);
183+
$this->performJoin($tableAs ?? $table, $foreign, $owner ?? $other, $deletedAt);
181184
$lastQuery = $model->getQuery();
182185
}
183186

184-
return $table . '.' . $relationColumn;
187+
$table = $alias ?? $table;
188+
189+
return "{$table}.{$relationColumn}";
185190
}
186191

187192
protected function checkSoftDeletesOnModel($model)

tests/Integration/QueryDataTableTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function it_allows_search_on_added_column_with_custom_filter_handler()
218218
]);
219219

220220
$queries = $crawler->json()['queries'];
221-
$this->assertContains('"1" = ?', $queries[1]['query']);
221+
$this->assertStringContainsString('"1" = ?', $queries[1]['query']);
222222
}
223223

224224
protected function setUp(): void

0 commit comments

Comments
 (0)