Skip to content

Commit 7083b5c

Browse files
authored
Merge pull request #3252 from ejgandelaberon/fix-ambiguous-column
Fix ambiguous column in columnControlSearch() method
2 parents b5a58e0 + 85ce44d commit 7083b5c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/QueryDataTable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ public function columnControlSearch(): void
358358
continue;
359359
}
360360

361+
// Only resolve relation after checking for a custom filter.
362+
// Because the custom filter for a column might not be found, e.g., $this->hasFilterColumn($columnName)
363+
// and applyFilterColumn() already resolves relations
364+
$columnName = $this->resolveRelationColumn($columnName);
365+
361366
if ($list) {
362367
if (str_contains($logic, 'not')) {
363368
$this->query->whereNotIn($columnName, $list);

tests/Unit/QueryDataTableTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Facades\DB;
66
use Illuminate\Support\Str;
7+
use PHPUnit\Framework\Attributes\Test;
78
use Yajra\DataTables\Tests\Models\User;
89
use Yajra\DataTables\Tests\TestCase;
910

@@ -171,4 +172,42 @@ public function assertQueryHasNoSelect($expected, $query): void
171172

172173
$this->assertSame($expected, Str::startsWith($sql, 'select count(*) from (select 1 as dt_row_count from'), "'{$sql}' has select");
173174
}
175+
176+
#[Test]
177+
public function test_column_name_is_resolved_in_column_control(): void
178+
{
179+
app('datatables.request')->merge([
180+
'columns' => [
181+
[
182+
'name' => 'id',
183+
'data' => 'id',
184+
'searchable' => 'true',
185+
'orderable' => 'true',
186+
'search' => ['value' => null, 'regex' => 'false'],
187+
'columnControl' => [
188+
'search' => [
189+
'value' => '123',
190+
'logic' => 'equal',
191+
'type' => 'num',
192+
],
193+
],
194+
],
195+
],
196+
]);
197+
198+
/** @var \Yajra\DataTables\QueryDataTable $dataTable */
199+
$dataTable = app('datatables')->of(
200+
User::query()
201+
->select('users.*')
202+
->join('role_user', 'users.id', '=', 'role_user.user_id')
203+
->join('roles', 'role_user.role_id', '=', 'roles.id')
204+
);
205+
206+
$dataTable->columnControlSearch();
207+
208+
$this->assertStringContainsString(
209+
'"users"."id" = \'123\'',
210+
$dataTable->getQuery()->toRawSql()
211+
);
212+
}
174213
}

0 commit comments

Comments
 (0)