Skip to content

Commit 672c402

Browse files
committed
feat: add option to disable alias on relation tables
1 parent f305033 commit 672c402

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

src/EloquentDataTable.php

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
*/
1818
class EloquentDataTable extends QueryDataTable
1919
{
20+
/**
21+
* Flag to disable the generation of unique table aliases on eagerly loaded join columns.
22+
*/
23+
protected bool $disableEagerJoinAliases = false;
24+
2025
/**
2126
* EloquentEngine constructor.
2227
*/
@@ -183,22 +188,34 @@ protected function resolveRelationColumn(string $column): string
183188
*/
184189
protected function joinEagerLoadedColumn($relation, $relationColumn)
185190
{
186-
$tableAlias = '';
191+
$tableAlias = $pivotAlias = '';
187192
$lastQuery = $this->query;
188193
foreach (explode('.', $relation) as $eachRelation) {
189194
$model = $lastQuery->getRelation($eachRelation);
190-
$lastAlias = $tableAlias ?: $this->getTablePrefix($lastQuery);
191-
$tableAlias = $tableAlias.'_'.$eachRelation;
192-
$pivotAlias = $tableAlias.'_pivot';
195+
if (! $this->disableEagerJoinAliases) {
196+
$lastAlias = $tableAlias ?: $this->getTablePrefix($lastQuery);
197+
$tableAlias = $tableAlias.'_'.$eachRelation;
198+
$pivotAlias = $tableAlias.'_pivot';
199+
} else {
200+
$lastAlias = $tableAlias ?: $lastQuery->getModel()->getTable();
201+
}
193202
switch (true) {
194203
case $model instanceof BelongsToMany:
195-
$pivot = $model->getTable().' as '.$pivotAlias;
204+
if (! $this->disableEagerJoinAliases) {
205+
$pivot = $model->getTable().' as '.$pivotAlias;
206+
} else {
207+
$pivot = $pivotAlias = $model->getTable();
208+
}
196209
$pivotPK = $pivotAlias.'.'.$model->getForeignPivotKeyName();
197210
$pivotFK = ltrim($lastAlias.'.'.$model->getParentKeyName(), '.');
198211
$this->performJoin($pivot, $pivotPK, $pivotFK);
199212

200213
$related = $model->getRelated();
201-
$table = $related->getTable().' as '.$tableAlias;
214+
if (! $this->disableEagerJoinAliases) {
215+
$table = $related->getTable().' as '.$tableAlias;
216+
} else {
217+
$table = $tableAlias = $related->getTable();
218+
}
202219
$tablePK = $model->getRelatedPivotKeyName();
203220
$foreign = $pivotAlias.'.'.$tablePK;
204221
$other = $tableAlias.'.'.$related->getKeyName();
@@ -208,13 +225,21 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
208225
break;
209226

210227
case $model instanceof HasOneThrough:
211-
$pivot = explode('.', $model->getQualifiedParentKeyName())[0].' as '.$pivotAlias; // extract pivot table from key
228+
if (! $this->disableEagerJoinAliases) {
229+
$pivot = explode('.', $model->getQualifiedParentKeyName())[0].' as '.$pivotAlias;
230+
} else {
231+
$pivot = $pivotAlias = explode('.', $model->getQualifiedParentKeyName())[0];
232+
}
212233
$pivotPK = $pivotAlias.'.'.$model->getFirstKeyName();
213234
$pivotFK = ltrim($lastAlias.'.'.$model->getLocalKeyName(), '.');
214235
$this->performJoin($pivot, $pivotPK, $pivotFK);
215236

216237
$related = $model->getRelated();
217-
$table = $related->getTable().' as '.$tableAlias;
238+
if (! $this->disableEagerJoinAliases) {
239+
$table = $related->getTable().' as '.$tableAlias;
240+
} else {
241+
$table = $tableAlias = $related->getTable();
242+
}
218243
$tablePK = $model->getSecondLocalKeyName();
219244
$foreign = $pivotAlias.'.'.$tablePK;
220245
$other = $tableAlias.'.'.$related->getKeyName();
@@ -224,13 +249,21 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
224249
break;
225250

226251
case $model instanceof HasOneOrMany:
227-
$table = $model->getRelated()->getTable().' as '.$tableAlias;
252+
if (! $this->disableEagerJoinAliases) {
253+
$table = $model->getRelated()->getTable().' as '.$tableAlias;
254+
} else {
255+
$table = $tableAlias = $model->getRelated()->getTable();
256+
}
228257
$foreign = $tableAlias.'.'.$model->getForeignKeyName();
229258
$other = ltrim($lastAlias.'.'.$model->getLocalKeyName(), '.');
230259
break;
231260

232261
case $model instanceof BelongsTo:
233-
$table = $model->getRelated()->getTable().' as '.$tableAlias;
262+
if (! $this->disableEagerJoinAliases) {
263+
$table = $model->getRelated()->getTable().' as '.$tableAlias;
264+
} else {
265+
$table = $tableAlias = $model->getRelated()->getTable();
266+
}
234267
$foreign = ltrim($lastAlias.'.'.$model->getForeignKeyName(), '.');
235268
$other = $tableAlias.'.'.$model->getOwnerKeyName();
236269
break;
@@ -245,6 +278,18 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
245278
return $tableAlias.'.'.$relationColumn;
246279
}
247280

281+
/**
282+
* Disable the generation of unique table aliases on eagerly loaded join columns.
283+
*
284+
* @return $this
285+
*/
286+
public function disableEagerJoinAliases(): static
287+
{
288+
$this->disableEagerJoinAliases = true;
289+
290+
return $this;
291+
}
292+
248293
/**
249294
* Perform join query.
250295
*

0 commit comments

Comments
 (0)