17
17
*/
18
18
class EloquentDataTable extends QueryDataTable
19
19
{
20
+ /**
21
+ * Flag to disable the generation of unique table aliases on eagerly loaded join columns.
22
+ */
23
+ protected bool $ disableEagerJoinAliases = false ;
24
+
20
25
/**
21
26
* EloquentEngine constructor.
22
27
*/
@@ -183,22 +188,34 @@ protected function resolveRelationColumn(string $column): string
183
188
*/
184
189
protected function joinEagerLoadedColumn ($ relation , $ relationColumn )
185
190
{
186
- $ tableAlias = '' ;
191
+ $ tableAlias = $ pivotAlias = '' ;
187
192
$ lastQuery = $ this ->query ;
188
193
foreach (explode ('. ' , $ relation ) as $ eachRelation ) {
189
194
$ 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
+ }
193
202
switch (true ) {
194
203
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
+ }
196
209
$ pivotPK = $ pivotAlias .'. ' .$ model ->getForeignPivotKeyName ();
197
210
$ pivotFK = ltrim ($ lastAlias .'. ' .$ model ->getParentKeyName (), '. ' );
198
211
$ this ->performJoin ($ pivot , $ pivotPK , $ pivotFK );
199
212
200
213
$ 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
+ }
202
219
$ tablePK = $ model ->getRelatedPivotKeyName ();
203
220
$ foreign = $ pivotAlias .'. ' .$ tablePK ;
204
221
$ other = $ tableAlias .'. ' .$ related ->getKeyName ();
@@ -208,13 +225,21 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
208
225
break ;
209
226
210
227
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
+ }
212
233
$ pivotPK = $ pivotAlias .'. ' .$ model ->getFirstKeyName ();
213
234
$ pivotFK = ltrim ($ lastAlias .'. ' .$ model ->getLocalKeyName (), '. ' );
214
235
$ this ->performJoin ($ pivot , $ pivotPK , $ pivotFK );
215
236
216
237
$ 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
+ }
218
243
$ tablePK = $ model ->getSecondLocalKeyName ();
219
244
$ foreign = $ pivotAlias .'. ' .$ tablePK ;
220
245
$ other = $ tableAlias .'. ' .$ related ->getKeyName ();
@@ -224,13 +249,21 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
224
249
break ;
225
250
226
251
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
+ }
228
257
$ foreign = $ tableAlias .'. ' .$ model ->getForeignKeyName ();
229
258
$ other = ltrim ($ lastAlias .'. ' .$ model ->getLocalKeyName (), '. ' );
230
259
break ;
231
260
232
261
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
+ }
234
267
$ foreign = ltrim ($ lastAlias .'. ' .$ model ->getForeignKeyName (), '. ' );
235
268
$ other = $ tableAlias .'. ' .$ model ->getOwnerKeyName ();
236
269
break ;
@@ -245,6 +278,18 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
245
278
return $ tableAlias .'. ' .$ relationColumn ;
246
279
}
247
280
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
+
248
293
/**
249
294
* Perform join query.
250
295
*
0 commit comments