How do I filter count columns
I have a column to display member counts of a group.
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.
Thanks
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()
,
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)
);
}),
1 Reply
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