Skip to content

fix: prevent duplicate table name errors #3216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions src/EloquentDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected function resolveRelationColumn(string $column): string
{
$parts = explode('.', $column);
$columnName = array_pop($parts);
$relation = implode('.', $parts);
$relation = str_replace('[]', '', implode('.', $parts));

if ($this->isNotEagerLoaded($relation)) {
return $column;
Expand All @@ -184,54 +184,56 @@ protected function resolveRelationColumn(string $column): string
*/
protected function joinEagerLoadedColumn($relation, $relationColumn)
{
$table = '';
$tableAlias = '';
$lastQuery = $this->query;
foreach (explode('.', $relation) as $eachRelation) {
$model = $lastQuery->getRelation($eachRelation);
$lastAlias = $tableAlias ?: $lastQuery->getModel()->getTable();
$tableAlias = $tableAlias.'_'.$eachRelation;
$pivotAlias = $tableAlias.'_pivot';
switch (true) {
case $model instanceof BelongsToMany:
$pivot = $model->getTable();
$pivotPK = $model->getExistenceCompareKey();
$pivotFK = $model->getQualifiedParentKeyName();
$pivot = $model->getTable().' as '.$pivotAlias;
$pivotPK = $pivotAlias.'.'.$model->getForeignPivotKeyName();
$pivotFK = $lastAlias.'.'.$model->getParentKeyName();
$this->performJoin($pivot, $pivotPK, $pivotFK);

$related = $model->getRelated();
$table = $related->getTable();
$table = $related->getTable().' as '.$tableAlias;
$tablePK = $model->getRelatedPivotKeyName();
$foreign = $pivot.'.'.$tablePK;
$other = $related->getQualifiedKeyName();
$foreign = $pivotAlias.'.'.$tablePK;
$other = $tableAlias.'.'.$related->getKeyName();

$lastQuery->addSelect($table.'.'.$relationColumn);
$this->performJoin($table, $foreign, $other);
$lastQuery->addSelect($tableAlias.'.'.$relationColumn);

break;

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

$related = $model->getRelated();
$table = $related->getTable();
$table = $related->getTable().' as '.$tableAlias;
$tablePK = $model->getSecondLocalKeyName();
$foreign = $pivot.'.'.$tablePK;
$other = $related->getQualifiedKeyName();
$foreign = $pivotAlias.'.'.$tablePK;
$other = $tableAlias.'.'.$related->getKeyName();

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

break;

case $model instanceof HasOneOrMany:
$table = $model->getRelated()->getTable();
$foreign = $model->getQualifiedForeignKeyName();
$other = $model->getQualifiedParentKeyName();
$table = $model->getRelated()->getTable().' as '.$tableAlias;
$foreign = $tableAlias.'.'.$model->getForeignKeyName();
$other = $lastAlias.'.'.$model->getLocalKeyName();
break;

case $model instanceof BelongsTo:
$table = $model->getRelated()->getTable();
$foreign = $model->getQualifiedForeignKeyName();
$other = $model->getQualifiedOwnerKeyName();
$table = $model->getRelated()->getTable().' as '.$tableAlias;
$foreign = $lastAlias.'.'.$model->getForeignKeyName();
$other = $tableAlias.'.'.$model->getOwnerKeyName();
break;

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

return $table.'.'.$relationColumn;
return $tableAlias.'.'.$relationColumn;
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,6 @@ protected function defaultOrdering(): void
$column = $this->resolveRelationColumn($orderable['name']);

if ($this->hasOrderColumn($orderable['name'])) {
$this->applyOrderColumn($orderable['name'], $orderable);
} elseif ($this->hasOrderColumn($column)) {
$this->applyOrderColumn($column, $orderable);
} else {
$nullsLastSql = $this->getNullsLastSql($column, $orderable['direction']);
Expand All @@ -687,16 +685,16 @@ protected function hasOrderColumn(string $column): bool
*/
protected function applyOrderColumn(string $column, array $orderable): void
{
$sql = $this->columnDef['order'][$column]['sql'];
$sql = $this->columnDef['order'][$orderable['name']]['sql'];
if ($sql === false) {
return;
}

if (is_callable($sql)) {
call_user_func($sql, $this->query, $orderable['direction']);
call_user_func($sql, $this->query, $orderable['direction'], $column);
} else {
$sql = str_replace('$1', $orderable['direction'], (string) $sql);
$bindings = $this->columnDef['order'][$column]['bindings'];
$bindings = $this->columnDef['order'][$orderable['name']]['bindings'];
$this->query->orderByRaw($sql, $bindings);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/CustomOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ protected function setUp(): void
parent::setUp();

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