File tree Expand file tree Collapse file tree 2 files changed +29
-3
lines changed Expand file tree Collapse file tree 2 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,11 @@ class QueryDataTable extends DataTableAbstract
25
25
*/
26
26
protected bool $ prepared = false ;
27
27
28
+ /**
29
+ * Flag to check if the total records count query has been performed.
30
+ */
31
+ protected bool $ performedTotalRecordsCount = false ;
32
+
28
33
/**
29
34
* Query callback for custom pagination using limit without offset.
30
35
*
@@ -157,6 +162,20 @@ public function prepareQuery(): static
157
162
return $ this ;
158
163
}
159
164
165
+ /**
166
+ * Count total items.
167
+ */
168
+ public function totalCount (): int
169
+ {
170
+ if ($ this ->totalRecords !== null ) {
171
+ return $ this ->totalRecords ;
172
+ }
173
+
174
+ $ this ->performedTotalRecordsCount = true ;
175
+
176
+ return $ this ->totalRecords = $ this ->count ();
177
+ }
178
+
160
179
/**
161
180
* Counts current query.
162
181
*/
@@ -253,7 +272,7 @@ protected function filterRecords(): void
253
272
254
273
// If no modification between the original query and the filtered one has been made
255
274
// the filteredRecords equals the totalRecords
256
- if ($ this ->query == $ initialQuery ) {
275
+ if ($ this ->query == $ initialQuery && $ this -> performedTotalRecordsCount ) {
257
276
$ this ->filteredRecords ??= $ this ->totalRecords ;
258
277
} else {
259
278
$ this ->filteredCount ();
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ public function it_can_set_total_records()
26
26
$ crawler ->assertJson ([
27
27
'draw ' => 0 ,
28
28
'recordsTotal ' => 10 ,
29
- 'recordsFiltered ' => 10 ,
29
+ 'recordsFiltered ' => 20 ,
30
30
]);
31
31
}
32
32
@@ -37,7 +37,7 @@ public function it_can_set_zero_total_records()
37
37
$ crawler ->assertJson ([
38
38
'draw ' => 0 ,
39
39
'recordsTotal ' => 0 ,
40
- 'recordsFiltered ' => 0 ,
40
+ 'recordsFiltered ' => 20 ,
41
41
]);
42
42
}
43
43
@@ -55,12 +55,19 @@ public function it_can_set_total_filtered_records()
55
55
#[Test]
56
56
public function it_returns_all_records_when_no_parameters_is_passed ()
57
57
{
58
+ DB ::enableQueryLog ();
59
+
58
60
$ crawler = $ this ->call ('GET ' , '/query/users ' );
59
61
$ crawler ->assertJson ([
60
62
'draw ' => 0 ,
61
63
'recordsTotal ' => 20 ,
62
64
'recordsFiltered ' => 20 ,
63
65
]);
66
+
67
+ DB ::disableQueryLog ();
68
+ $ queryLog = DB ::getQueryLog ();
69
+
70
+ $ this ->assertCount (2 , $ queryLog );
64
71
}
65
72
66
73
#[Test]
You can’t perform that action at this time.
0 commit comments