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
So where is your software_count coming from? Are you relying on Laravel's aggregate functions?
yeah & using https://filamentphp.com/docs/3.x/tables/columns/relationships#counting-relationships for the column. i assume ill have to take a different approach
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 ...
... 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?