Skip to content

Commit 1b026ba

Browse files
committed
fix: applyOrderColumn with column alias
1 parent c8af15e commit 1b026ba

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

src/QueryDataTable.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,6 @@ protected function defaultOrdering(): void
662662
$column = $this->resolveRelationColumn($orderable['name']);
663663

664664
if ($this->hasOrderColumn($orderable['name'])) {
665-
$this->applyOrderColumn($orderable['name'], $orderable);
666-
} elseif ($this->hasOrderColumn($column)) {
667665
$this->applyOrderColumn($column, $orderable);
668666
} else {
669667
$nullsLastSql = $this->getNullsLastSql($column, $orderable['direction']);
@@ -687,16 +685,16 @@ protected function hasOrderColumn(string $column): bool
687685
*/
688686
protected function applyOrderColumn(string $column, array $orderable): void
689687
{
690-
$sql = $this->columnDef['order'][$column]['sql'];
688+
$sql = $this->columnDef['order'][$orderable['name']]['sql'];
691689
if ($sql === false) {
692690
return;
693691
}
694692

695693
if (is_callable($sql)) {
696-
call_user_func($sql, $this->query, $orderable['direction']);
694+
call_user_func($sql, $this->query, $orderable['direction'], $column);
697695
} else {
698696
$sql = str_replace('$1', $orderable['direction'], (string) $sql);
699-
$bindings = $this->columnDef['order'][$column]['bindings'];
697+
$bindings = $this->columnDef['order'][$orderable['name']]['bindings'];
700698
$this->query->orderByRaw($sql, $bindings);
701699
}
702700
}

tests/Integration/CustomOrderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ protected function setUp(): void
5454
parent::setUp();
5555

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

0 commit comments

Comments
 (0)