F
Filament15mo ago
Log1x

Sort resource by relationship count – exception when empty

I have Developers that own Software and I'm currently using $table->defaultSort('software_count', 'desc') to sort my developers by the amount of software they have on my DeveloperResource. If a developer does not have any software, I get an SQL exception for Unknown column 'software_count' in 'order clause' Is there any way around that? It's just a simple HasMany relationship. I get this same issue with Tables\Columns\TextColumn::make('software_count') but I think I can maybe do a hidden() conditional check on it or something – but I also feel like I might be missing something. Ideally it'd just default to 0.
3 Replies
cheesegrits
cheesegrits15mo ago
So where is your software_count coming from? Are you relying on Laravel's aggregate functions?
cheesegrits
cheesegrits15mo ago
It's odd. I would have expected it to either work or not work, not just error out when there is no relationships to count. So to make sure I understand ... you have a TextColumn like this ...
Tables\Columns\TextColumn::make('software_count')
->counts('software');
Tables\Columns\TextColumn::make('software_count')
->counts('software');
... that counts a 'software' relationship on your Developer model. And it works fine if all developers in the displayed table have a software count, but blows up if any of them don't?
Want results from more Discord servers?
Add your server