Skip to content

Commit

Permalink
Merge pull request #3026 from Naghal/FixQueryDatatableCount
Browse files Browse the repository at this point in the history
Fix #3025
  • Loading branch information
yajra authored Jul 5, 2023
2 parents 66299d9 + 15930cc commit 079cd4b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function prepareCountQuery(): QueryBuilder
$builder = clone $this->query;

if ($this->isComplexQuery($builder)) {
$builder->select(DB::raw('1'));
$builder->select(DB::raw('1 as dt_row_count'));
if ($this->ignoreSelectInCountQuery || ! $this->isComplexQuery($builder)) {
return $this->getConnection()
->query()
Expand Down
8 changes: 5 additions & 3 deletions tests/Unit/QueryDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function test_complex_query_use_select_in_count()
->orderBy('created_at')
->select('id'),
])
->orderBy(DB::table('posts')->whereColumn('posts.user_id', 'users.id')->orderBy('created_at')->select('created_at')
->orderBy(
DB::table('posts')->whereColumn('posts.user_id', 'users.id')->orderBy('created_at')->select('created_at')
)
);

Expand All @@ -64,7 +65,8 @@ public function test_complex_query_can_ignore_select_in_count()
->orderBy('created_at')
->select('id'),
])
->orderBy(DB::table('posts')->whereColumn('posts.user_id', 'users.id')->orderBy('created_at')->select('created_at')
->orderBy(
DB::table('posts')->whereColumn('posts.user_id', 'users.id')->orderBy('created_at')->select('created_at')
)
)->ignoreSelectsInCountQuery();

Expand Down Expand Up @@ -134,6 +136,6 @@ public function assertQueryHasNoSelect($expected, $query)
{
$sql = $query->toSql();

$this->assertSame($expected, Str::startsWith($sql, 'select * from (select 1 from'), "'{$sql}' is not wrapped");
$this->assertSame($expected, Str::startsWith($sql, 'select * from (select 1 as dt_row_count from'), "'{$sql}' is not wrapped");
}
}

0 comments on commit 079cd4b

Please sign in to comment.