Skip to content
Merged
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
67 changes: 57 additions & 10 deletions src/EloquentDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
*/
class EloquentDataTable extends QueryDataTable
{
/**
* Flag to enable the generation of unique table aliases on eagerly loaded join columns.
* You may want to enable it if you encounter a "Not unique table/alias" error when performing a search or applying ordering.
*/
protected bool $enableEagerJoinAliases = false;

/**
* EloquentEngine constructor.
*/
Expand Down Expand Up @@ -183,22 +189,34 @@ protected function resolveRelationColumn(string $column): string
*/
protected function joinEagerLoadedColumn($relation, $relationColumn)
{
$tableAlias = '';
$tableAlias = $pivotAlias = '';
$lastQuery = $this->query;
foreach (explode('.', $relation) as $eachRelation) {
$model = $lastQuery->getRelation($eachRelation);
$lastAlias = $tableAlias ?: $this->getTablePrefix($lastQuery);
$tableAlias = $tableAlias.'_'.$eachRelation;
$pivotAlias = $tableAlias.'_pivot';
if ($this->enableEagerJoinAliases) {
$lastAlias = $tableAlias ?: $this->getTablePrefix($lastQuery);
$tableAlias = $tableAlias.'_'.$eachRelation;
$pivotAlias = $tableAlias.'_pivot';
} else {
$lastAlias = $tableAlias ?: $lastQuery->getModel()->getTable();
}
switch (true) {
case $model instanceof BelongsToMany:
$pivot = $model->getTable().' as '.$pivotAlias;
if ($this->enableEagerJoinAliases) {
$pivot = $model->getTable().' as '.$pivotAlias;
} else {
$pivot = $pivotAlias = $model->getTable();
}
$pivotPK = $pivotAlias.'.'.$model->getForeignPivotKeyName();
$pivotFK = ltrim($lastAlias.'.'.$model->getParentKeyName(), '.');
$this->performJoin($pivot, $pivotPK, $pivotFK);

$related = $model->getRelated();
$table = $related->getTable().' as '.$tableAlias;
if ($this->enableEagerJoinAliases) {
$table = $related->getTable().' as '.$tableAlias;
} else {
$table = $tableAlias = $related->getTable();
}
$tablePK = $model->getRelatedPivotKeyName();
$foreign = $pivotAlias.'.'.$tablePK;
$other = $tableAlias.'.'.$related->getKeyName();
Expand All @@ -208,13 +226,21 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
break;

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

$related = $model->getRelated();
$table = $related->getTable().' as '.$tableAlias;
if ($this->enableEagerJoinAliases) {
$table = $related->getTable().' as '.$tableAlias;
} else {
$table = $tableAlias = $related->getTable();
}
$tablePK = $model->getSecondLocalKeyName();
$foreign = $pivotAlias.'.'.$tablePK;
$other = $tableAlias.'.'.$related->getKeyName();
Expand All @@ -224,13 +250,21 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
break;

case $model instanceof HasOneOrMany:
$table = $model->getRelated()->getTable().' as '.$tableAlias;
if ($this->enableEagerJoinAliases) {
$table = $model->getRelated()->getTable().' as '.$tableAlias;
} else {
$table = $tableAlias = $model->getRelated()->getTable();
}
$foreign = $tableAlias.'.'.$model->getForeignKeyName();
$other = ltrim($lastAlias.'.'.$model->getLocalKeyName(), '.');
break;

case $model instanceof BelongsTo:
$table = $model->getRelated()->getTable().' as '.$tableAlias;
if ($this->enableEagerJoinAliases) {
$table = $model->getRelated()->getTable().' as '.$tableAlias;
} else {
$table = $tableAlias = $model->getRelated()->getTable();
}
$foreign = ltrim($lastAlias.'.'.$model->getForeignKeyName(), '.');
$other = $tableAlias.'.'.$model->getOwnerKeyName();
break;
Expand All @@ -245,6 +279,19 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
return $tableAlias.'.'.$relationColumn;
}

/**
* Enable the generation of unique table aliases on eagerly loaded join columns.
* You may want to enable it if you encounter a "Not unique table/alias" error when performing a search or applying ordering.
*
* @return $this
*/
public function enableEagerJoinAliases(): static
{
$this->enableEagerJoinAliases = true;

return $this;
}

/**
* Perform join query.
*
Expand Down
Loading