F
Filament2y ago
Erik

Having in Table Filters

I am trying to filter a calculated field in a table. It is not finding the calculated field, which is why I have to use "HAVING". However, when I use it, it's not working. The builder is not adding "HAVING" to the query. How can I filter a calculated field in the query then?
1 Reply
Erik
ErikOP2y ago
The code would be the following:
Tables\Filters\Filter::make('netto_sum')
->form([
Forms\Components\Fieldset::make('Összeg (nettó)')->schema([
Forms\Components\TextInput::make('from')
->label('Ettől')
->nullable()
->columnSpan([
'default' => 12,
'md' => 12,
'lg' => 6,
]),
Forms\Components\TextInput::make('until')
->label('Eddig')
->nullable()
->columnSpan([
'default' => 12,
'md' => 12,
'lg' => 6,
]),
])->columns(12)
])
->query(function (Builder $query, array $data): Builder {

return $query
->when(
$data['from'],
fn (Builder $q, $data): Builder => $q->havingRaw('netto_sum >= ?', [0])
)
->when(
$data['until'],
fn (Builder $q, $data): Builder => $q->havingRaw('netto_sum <= ?', [3100])
);
}),
Tables\Filters\Filter::make('netto_sum')
->form([
Forms\Components\Fieldset::make('Összeg (nettó)')->schema([
Forms\Components\TextInput::make('from')
->label('Ettől')
->nullable()
->columnSpan([
'default' => 12,
'md' => 12,
'lg' => 6,
]),
Forms\Components\TextInput::make('until')
->label('Eddig')
->nullable()
->columnSpan([
'default' => 12,
'md' => 12,
'lg' => 6,
]),
])->columns(12)
])
->query(function (Builder $query, array $data): Builder {

return $query
->when(
$data['from'],
fn (Builder $q, $data): Builder => $q->havingRaw('netto_sum >= ?', [0])
)
->when(
$data['until'],
fn (Builder $q, $data): Builder => $q->havingRaw('netto_sum <= ?', [3100])
);
}),
Want results from more Discord servers?
Add your server