Filament overriding withCount ?

Hi everyone ! I'm currently trying to add a "tickets_count" attribute on a "users" relationship to use it with AND without Filament. So my relationship looks like this:
public function users() {
return $this->hasManyDeep(
User::class,
[OrderSession::class, Order::class, Basket::class],
[null, 'id', 'id', 'id'],
[null, 'order_id', 'basket_id', 'user_id']
)->distinct()
->withCount(['tickets as tickets_count' => function (Builder $query) {
$query->forSession($this)->withoutCanceled();
}]);
}
public function users() {
return $this->hasManyDeep(
User::class,
[OrderSession::class, Order::class, Basket::class],
[null, 'id', 'id', 'id'],
[null, 'order_id', 'basket_id', 'user_id']
)->distinct()
->withCount(['tickets as tickets_count' => function (Builder $query) {
$query->forSession($this)->withoutCanceled();
}]);
}
The problem is that when I use this relationship in a Relation Manager, my withCount call is not taken in account. It seems Filament is actually overriding my selected attributes (which could be quite logical). Is there any way to solve this ?
7 Replies
Dennis Koch
Dennis Koch3d ago
What does your Relation Manager look like?
Skythrew
SkythrewOP3d ago
Here's the full code:
class UsersRelationManager extends RelationManager
{
protected static string $relationship = 'users';

protected static ?string $title = 'Clients';

public function table(Table $table): Table
{
return $table
->modifyQueryUsing(function (Builder $query) {
/*$query->withCount(['tickets as tickets_count' => function (Builder $query) {
$query->forSession($this->getOwnerRecord())->withoutCanceled();
}]);*/
})
->recordTitleAttribute('name')
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('tickets_count')
]);
}
}
class UsersRelationManager extends RelationManager
{
protected static string $relationship = 'users';

protected static ?string $title = 'Clients';

public function table(Table $table): Table
{
return $table
->modifyQueryUsing(function (Builder $query) {
/*$query->withCount(['tickets as tickets_count' => function (Builder $query) {
$query->forSession($this->getOwnerRecord())->withoutCanceled();
}]);*/
})
->recordTitleAttribute('name')
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('tickets_count')
]);
}
}
And for now the only way I found to "bypass" Filament override is to add the withCount in the modifyQueryUsing table method as you can see 😅 Which is not very convenient
Dennis Koch
Dennis Koch3d ago
Tables\Columns\TextColumn::make('tickets_count')->counts('tickets') should be all you need
Dennis Koch
Dennis Koch3d ago
Or with the query you applied:
Tables\Columns\TextColumn::make('tickets_count')
->counts([
'tickets' => fn (Builder $query) => $query->forSession($this)->withoutCanceled()
])
Tables\Columns\TextColumn::make('tickets_count')
->counts([
'tickets' => fn (Builder $query) => $query->forSession($this)->withoutCanceled()
])
Skythrew
SkythrewOP3d ago
That's actually what I thought I should be using too but isn't there any way to use the field which is added directly by the relationship in order to avoid writing the same code in my table as in my model ?
Dennis Koch
Dennis Koch3d ago
You could try something like this:
$table
->query(Users::query()->where('client_id', $this->ownerRecord->id))
$table
->query(Users::query()->where('client_id', $this->ownerRecord->id))

Did you find this page helpful?