Skip to content

Commit d0baf8c

Browse files
authored
Merge pull request #3216 from Seb33300/fix-duplicate-table-names
fix: prevent duplicate table name errors
2 parents 4eec683 + 1b026ba commit d0baf8c

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

src/EloquentDataTable.php

+25-23
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ protected function resolveRelationColumn(string $column): string
164164
{
165165
$parts = explode('.', $column);
166166
$columnName = array_pop($parts);
167-
$relation = implode('.', $parts);
167+
$relation = str_replace('[]', '', implode('.', $parts));
168168

169169
if ($this->isNotEagerLoaded($relation)) {
170170
return $column;
@@ -184,54 +184,56 @@ protected function resolveRelationColumn(string $column): string
184184
*/
185185
protected function joinEagerLoadedColumn($relation, $relationColumn)
186186
{
187-
$table = '';
187+
$tableAlias = '';
188188
$lastQuery = $this->query;
189189
foreach (explode('.', $relation) as $eachRelation) {
190190
$model = $lastQuery->getRelation($eachRelation);
191+
$lastAlias = $tableAlias ?: $lastQuery->getModel()->getTable();
192+
$tableAlias = $tableAlias.'_'.$eachRelation;
193+
$pivotAlias = $tableAlias.'_pivot';
191194
switch (true) {
192195
case $model instanceof BelongsToMany:
193-
$pivot = $model->getTable();
194-
$pivotPK = $model->getExistenceCompareKey();
195-
$pivotFK = $model->getQualifiedParentKeyName();
196+
$pivot = $model->getTable().' as '.$pivotAlias;
197+
$pivotPK = $pivotAlias.'.'.$model->getForeignPivotKeyName();
198+
$pivotFK = $lastAlias.'.'.$model->getParentKeyName();
196199
$this->performJoin($pivot, $pivotPK, $pivotFK);
197200

198201
$related = $model->getRelated();
199-
$table = $related->getTable();
202+
$table = $related->getTable().' as '.$tableAlias;
200203
$tablePK = $model->getRelatedPivotKeyName();
201-
$foreign = $pivot.'.'.$tablePK;
202-
$other = $related->getQualifiedKeyName();
204+
$foreign = $pivotAlias.'.'.$tablePK;
205+
$other = $tableAlias.'.'.$related->getKeyName();
203206

204-
$lastQuery->addSelect($table.'.'.$relationColumn);
205-
$this->performJoin($table, $foreign, $other);
207+
$lastQuery->addSelect($tableAlias.'.'.$relationColumn);
206208

207209
break;
208210

209211
case $model instanceof HasOneThrough:
210-
$pivot = explode('.', $model->getQualifiedParentKeyName())[0]; // extract pivot table from key
211-
$pivotPK = $pivot.'.'.$model->getFirstKeyName();
212-
$pivotFK = $model->getQualifiedLocalKeyName();
212+
$pivot = explode('.', $model->getQualifiedParentKeyName())[0].' as '.$pivotAlias; // extract pivot table from key
213+
$pivotPK = $pivotAlias.'.'.$model->getFirstKeyName();
214+
$pivotFK = $lastAlias.'.'.$model->getLocalKeyName();
213215
$this->performJoin($pivot, $pivotPK, $pivotFK);
214216

215217
$related = $model->getRelated();
216-
$table = $related->getTable();
218+
$table = $related->getTable().' as '.$tableAlias;
217219
$tablePK = $model->getSecondLocalKeyName();
218-
$foreign = $pivot.'.'.$tablePK;
219-
$other = $related->getQualifiedKeyName();
220+
$foreign = $pivotAlias.'.'.$tablePK;
221+
$other = $tableAlias.'.'.$related->getKeyName();
220222

221223
$lastQuery->addSelect($lastQuery->getModel()->getTable().'.*');
222224

223225
break;
224226

225227
case $model instanceof HasOneOrMany:
226-
$table = $model->getRelated()->getTable();
227-
$foreign = $model->getQualifiedForeignKeyName();
228-
$other = $model->getQualifiedParentKeyName();
228+
$table = $model->getRelated()->getTable().' as '.$tableAlias;
229+
$foreign = $tableAlias.'.'.$model->getForeignKeyName();
230+
$other = $lastAlias.'.'.$model->getLocalKeyName();
229231
break;
230232

231233
case $model instanceof BelongsTo:
232-
$table = $model->getRelated()->getTable();
233-
$foreign = $model->getQualifiedForeignKeyName();
234-
$other = $model->getQualifiedOwnerKeyName();
234+
$table = $model->getRelated()->getTable().' as '.$tableAlias;
235+
$foreign = $lastAlias.'.'.$model->getForeignKeyName();
236+
$other = $tableAlias.'.'.$model->getOwnerKeyName();
235237
break;
236238

237239
default:
@@ -241,7 +243,7 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
241243
$lastQuery = $model->getQuery();
242244
}
243245

244-
return $table.'.'.$relationColumn;
246+
return $tableAlias.'.'.$relationColumn;
245247
}
246248

247249
/**

src/QueryDataTable.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,6 @@ protected function defaultOrdering(): void
662662
$column = $this->resolveRelationColumn($orderable['name']);
663663

664664
if ($this->hasOrderColumn($orderable['name'])) {
665-
$this->applyOrderColumn($orderable['name'], $orderable);
666-
} elseif ($this->hasOrderColumn($column)) {
667665
$this->applyOrderColumn($column, $orderable);
668666
} else {
669667
$nullsLastSql = $this->getNullsLastSql($column, $orderable['direction']);
@@ -687,16 +685,16 @@ protected function hasOrderColumn(string $column): bool
687685
*/
688686
protected function applyOrderColumn(string $column, array $orderable): void
689687
{
690-
$sql = $this->columnDef['order'][$column]['sql'];
688+
$sql = $this->columnDef['order'][$orderable['name']]['sql'];
691689
if ($sql === false) {
692690
return;
693691
}
694692

695693
if (is_callable($sql)) {
696-
call_user_func($sql, $this->query, $orderable['direction']);
694+
call_user_func($sql, $this->query, $orderable['direction'], $column);
697695
} else {
698696
$sql = str_replace('$1', $orderable['direction'], (string) $sql);
699-
$bindings = $this->columnDef['order'][$column]['bindings'];
697+
$bindings = $this->columnDef['order'][$orderable['name']]['bindings'];
700698
$this->query->orderByRaw($sql, $bindings);
701699
}
702700
}

tests/Integration/CustomOrderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ protected function setUp(): void
5454
parent::setUp();
5555

5656
$this->app['router']->get('/relations/belongsTo', fn (DataTables $datatables) => $datatables->eloquent(Post::with('user')->select('posts.*'))
57-
->orderColumn('user.id', function ($query, $order) {
58-
$query->orderBy('users.id', $order == 'desc' ? 'asc' : 'desc');
57+
->orderColumn('user.id', function ($query, $order, $column) {
58+
$query->orderBy($column, $order == 'desc' ? 'asc' : 'desc');
5959
})
6060
->toJson());
6161
}

0 commit comments

Comments
 (0)