Filter using columns

Hello, I'm having difficulty creating a filter in a query that I inserted in the columns. How can I retrieve the filter values and insert them into the query? Filament v3.1 public static function table(Table $table): Table { return $table ->defaultSort('created_at', 'desc') ->columns([ Tables\Columns\TextColumn::make('name') ->searchable(), Tables\Columns\TextColumn::make('total') ->getStateUsing(function (Model $record) { //FILTER HERE >>>>>>>>>>>>>>>>>>>>>>> $filter = $filters['tableFilters']['created_at']; $from = $filter['created_from']; $to = $filter['created_until']; //END FILTER HERE >>>>>>>>>>>>>>>>>>>>>>> return Deposit::where('user_id', $record->id) ->where('status', 1) ->whereDate('created_at', '>=', isset($from) && $from != 'null' ? Carbon::parse($from) : date("Y-m-d")) ->whereDate('created_at', '<=', isset($to) && $to != 'null' ? Carbon::parse($to) : date("Y-m-d")) ->sum('amount'); }) ->label(trans('Total')), ]) ->filters([ Filter::make('created_at') ->form([ DatePicker::make('created_from')->label('Initial'), DatePicker::make('created_until')->label('Final'), ]) ->indicateUsing(function (array $data): array { // I REMOVED BECAUSE LENGHT 2000.......... }) ]); }
Solution:
@lucasdutra04 I got my issue fixed like this ``` return $table ->query(...
Jump to solution
3 Replies
neerajk
neerajk9mo ago
any solution? @lucasdutra04
lucasdutra04
lucasdutra04OP9mo ago
No
Solution
neerajk
neerajk9mo ago
@lucasdutra04 I got my issue fixed like this
return $table
->query(
function () {
$filters = $this->tableFilters;

$dateRange = [];
if (!empty($filters['created_at']['created_from'])) {
$dateRange['from'] = $filters['created_at']['created_from'];
}
if (!empty($filters['created_at']['created_to'])) {
$dateRange['to'] = $filters['created_at']['created_to'];
}

return MyService::myCustomQuery($dateRange);
}
)
->columns([
Tables\Columns\TextColumn::make('id'),
Tables\Columns\TextColumn::make('name')->label('Name'),
...
])
->filters([
Tables\Filters\Filter::make('created_at')
->form([
DatePicker::make('created_from'),
DatePicker::make('created_to'),
])
]);
return $table
->query(
function () {
$filters = $this->tableFilters;

$dateRange = [];
if (!empty($filters['created_at']['created_from'])) {
$dateRange['from'] = $filters['created_at']['created_from'];
}
if (!empty($filters['created_at']['created_to'])) {
$dateRange['to'] = $filters['created_at']['created_to'];
}

return MyService::myCustomQuery($dateRange);
}
)
->columns([
Tables\Columns\TextColumn::make('id'),
Tables\Columns\TextColumn::make('name')->label('Name'),
...
])
->filters([
Tables\Filters\Filter::make('created_at')
->form([
DatePicker::make('created_from'),
DatePicker::make('created_to'),
])
]);
Want results from more Discord servers?
Add your server