Skip to content

Commit 562552b

Browse files
committed
fix: skip prefix on alias columns
1 parent c860947 commit 562552b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/QueryDataTable.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,23 @@ protected function compileQuerySearch($query, string $column, string $keyword, s
470470
*/
471471
protected function addTablePrefix($query, string $column): string
472472
{
473-
if (! str_contains($column, '.')) {
474-
return ltrim($this->getTablePrefix($query).'.'.$column, '.');
473+
// Column is already prefixed
474+
if (str_contains($column, '.')) {
475+
return $column;
475476
}
476477

477-
return $column;
478+
$q = $this->getBaseQueryBuilder($query);
479+
480+
// Column is an alias, no prefix required
481+
foreach ($q->columns ?? [] as $select) {
482+
$sql = trim($select instanceof Expression ? $select->getValue($this->getConnection()->getQueryGrammar()) : $select);
483+
if (str_ends_with($sql, ' as '.$column) || str_ends_with($sql, ' as '.$this->wrap($column))) {
484+
return $column;
485+
}
486+
}
487+
488+
// Add table prefix to column
489+
return $this->getTablePrefix($query).'.'.$column;
478490
}
479491

480492
/**

0 commit comments

Comments
 (0)