Skip to content

Commit a83cc54

Browse files
authored
Merge pull request #2806 from yajra/fix-2793
[10.x] Fix null recordsFiltered on empty collection
2 parents caa6381 + 57eadde commit a83cc54

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/DataTableAbstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ protected function render(array $data): JsonResponse
869869
$output = $this->attachAppends([
870870
'draw' => $this->request->draw(),
871871
'recordsTotal' => $this->totalRecords,
872-
'recordsFiltered' => $this->filteredRecords,
872+
'recordsFiltered' => $this->filteredRecords ?? 0,
873873
'data' => $data,
874874
]);
875875

tests/Integration/CollectionDataTableTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ public function it_returns_all_records_when_no_parameters_is_passed()
2525
]);
2626
}
2727

28+
/** @test */
29+
public function it_returns_zero_filtered_records_on_empty_collection()
30+
{
31+
$crawler = $this->call('GET', '/collection/empty');
32+
$crawler->assertExactJson([
33+
"data" => [],
34+
"draw" => 0,
35+
"input" => [],
36+
'recordsTotal' => 0,
37+
'recordsFiltered' => 0,
38+
]);
39+
}
40+
2841
/** @test */
2942
public function it_can_perform_global_search()
3043
{
@@ -242,5 +255,9 @@ protected function setUp(): void
242255
$this->app['router']->get('/collection/users', function (DataTables $datatables) {
243256
return $datatables->collection(User::all())->toJson();
244257
});
258+
259+
$this->app['router']->get('/collection/empty', function (DataTables $datatables) {
260+
return $datatables->collection([])->toJson();
261+
});
245262
}
246263
}

0 commit comments

Comments
 (0)