F
Filament2mo ago
Lloyd

TableWidget will not sort with getStateUsing

Hi all, I can't seem to sort my data when I am populating it with getStateUsing under a column that isn't in the model, here is my code:
TextColumn::make('matches')
->label('Matches')
->getStateUsing(function ($record) {
$matchCount = Matches::join('books', 'books.owner', '=', 'author.id')
->join('author_info', 'books.id', '=', 'author_info.host_id')
->where('author_info.prefix', '=', $record->prefix)
->count();

return $matchCount;
})
->sortable()
])
->defaultSort('matches', 'desc');
TextColumn::make('matches')
->label('Matches')
->getStateUsing(function ($record) {
$matchCount = Matches::join('books', 'books.owner', '=', 'author.id')
->join('author_info', 'books.id', '=', 'author_info.host_id')
->where('author_info.prefix', '=', $record->prefix)
->count();

return $matchCount;
})
->sortable()
])
->defaultSort('matches', 'desc');
The data is there, and is returning what I am expecting it to return. Even if I try and sort manually using the UI, it is not working. Can someone please help me? Thanks in advance : ) Note, this isn't in my model
3 Replies
Lloyd
LloydOP2mo ago
No description
awcodes
awcodes2mo ago
Sorting happens at the db level but you are overriding the db results with the getStateUsing() it’s too late in the lifecycle to override correctly.
Povilas K
Povilas K2mo ago
@Lloyd you would need to transform your table query to get that data BEFORE you put it into the table. Likely with some raw queries, attaching a similar example from one of our projects that may give you some ideas
No description

Did you find this page helpful?