Hi,
I have been trying to get this to work but im stuck.
Basically i got a relation on my record called 'animals' and on the table i need to have several counts of this object with different conditions, like so:
Tables\Columns\TextColumn::make('active_animals')
->getStateUsing(fn (Family $record): string => $record->animals()->where('active', true)->count())
->sortable(query: function (Builder $query, string $direction): Builder {
return $query
->orderBy('animals_count', $direction);
})->label('Aantal actieve dieren'),
Initially i had a custom attribute:
public function getActiveAnimalsCountAttribute(): string
{
return $this->animals()->where('active', true)->count();
}
But i was unable to sort on this.
Then i figured maybe im able to push this logic to the database and add a virtual column with the count where active is true. But couldn't find any resource on how to do this.
So now i tried the sortable function but somehow when i sort it orders by the actual 'animals_count' (makes sense given the function). But this discards the "where animal is active".
Basically now im stuck and have not really an idea how to move forward... Anyone has experience with this of has some ideas?