fix: enable sorting on computed records_count column in top teams widget#130
Conversation
Extract getDateRange() so the records_count column can use a custom sortable query with orderByRaw, fixing sorting on the computed subquery.
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where sorting on the computed records_count column in the Top Teams widget failed silently. The fix extracts the date range logic into a reusable getDateRange() method and implements a custom sortable closure that uses the same SQL subquery expression used to compute the column value.
Changes:
- Extracted
getDateRange()method to enable reuse of date range logic - Added custom sortable closure to the
records_countcolumn usingorderByRawwith parameterized bindings - Ensures sorting on computed column works correctly by mirroring the calculation logic used in the query
| ->sortable(query: function (Builder $query, string $direction): Builder { | ||
| [$sql, $bindings] = $this->buildRecordsCountExpression( | ||
| CreationSource::SYSTEM->value, | ||
| ...array_values($this->getDateRange()), | ||
| ); | ||
|
|
||
| return $query->orderByRaw("({$sql}) {$direction}", $bindings); | ||
| }) |
There was a problem hiding this comment.
The sorting functionality on the records_count column lacks test coverage. Based on the testing patterns established for other Filament resources in this codebase (e.g., CompanyResourceTest, NoteResourceTest), you should add a test that verifies sorting works correctly in both ascending and descending directions. Consider creating a test file for this widget that tests the sortable column behavior, similar to how other resources test their sortable columns using livewire()->sortTable() and assertCanSeeTableRecords().
Summary
getDateRange()method frombuildQuery()so it can be reused in the column's custom sortable querysortable()closure on therecords_countcolumn usingorderByRawwith the same subquery expressionTest plan