F
Filament2mo ago
Jon

How do I filter count columns

I have a column to display member counts of a group.
TextColumn::make('active_members_count')
->label('Members')
->counts('activeMembers')
->sortable()
->grow(false)
->wrapHeader()
,
TextColumn::make('active_members_count')
->label('Members')
->counts('activeMembers')
->sortable()
->grow(false)
->wrapHeader()
,
I am attempting to allow a filter that affects this column, but I am not sure the proper way to reference it as it is a calculated value.
Filter::make('active_members_count')
->form([
Fieldset::make('Members')
->schema([
TextInput::make('min')->numeric()
->label('Min Members'),
TextInput::make('max')->numeric()
->label('Max Members'),
]),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['min'],
fn(
Builder $query,
$members
): Builder => $query->having('active_members_count',
'>=', $members)
)
->when(
$data['max'],
fn(
Builder $query,
$members
): Builder => $query->having('active_members_count',
'<=', $members)
);
}),
Filter::make('active_members_count')
->form([
Fieldset::make('Members')
->schema([
TextInput::make('min')->numeric()
->label('Min Members'),
TextInput::make('max')->numeric()
->label('Max Members'),
]),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['min'],
fn(
Builder $query,
$members
): Builder => $query->having('active_members_count',
'>=', $members)
)
->when(
$data['max'],
fn(
Builder $query,
$members
): Builder => $query->having('active_members_count',
'<=', $members)
);
}),
Thanks
1 Reply
Jon
JonOP2mo ago
i dont even know if having is a valid function -- suggested by github copilot. but if this can be accomplished without running a bunch of queries again, and just use the already counted records in the table. that is what I would like. I'm just not sure how to reference that column properly
Want results from more Discord servers?
Add your server