F
Filament14mo ago
Skjena

How can I access the table filter value within the getTableQuery() function of a table widget?

Inside getTableFilters(), I created my filter fields and want to get the filter value within the getTableQuery() function to customize the table query accordingly.
12 Replies
ziolupo
ziolupo14mo ago
You can access both the query and the filters: $query=$this->getFilteredTableQuery(); $filters=$this->table->getFilters();
Skjena
SkjenaOP14mo ago
$query=$this->getFilteredTableQuery() returns the same response $table must not be accessed before initialization and $filters=$this->table->getFilters(); returns empty. To create a custom table widget, I actually made a trait and overrode the getTableQuery(), getTableFilters(), and makeTable() functions. Inside getTableFilters(), I created my filter fields and want to get the filter value within the getTableQuery() function to customize the table query. Here is my trait code
trait HasCustomCalendar
{
public function makeTable(): Table
{
$table = parent::makeTable();
return $table;
}

protected function getTableQuery(): Builder
{
$month_selected = $this->getTableFilterState("attendance_month")["value"];

}

protected function getTableFilters(): array
{
return [

Filter::make('attendance_month')
->form([
Select::make('value')
->disablePlaceholderSelection()
->options($months_list)
->default($today->format('Y-m-01')),
])
];
}
}
trait HasCustomCalendar
{
public function makeTable(): Table
{
$table = parent::makeTable();
return $table;
}

protected function getTableQuery(): Builder
{
$month_selected = $this->getTableFilterState("attendance_month")["value"];

}

protected function getTableFilters(): array
{
return [

Filter::make('attendance_month')
->form([
Select::make('value')
->disablePlaceholderSelection()
->options($months_list)
->default($today->format('Y-m-01')),
])
];
}
}
ziolupo
ziolupo14mo ago
I'm using a different approach. I'm taking the query already filtered and then I'm adding what I need. Here it is a full example: Action::make('Export') ->icon('heroicon-o-arrow-down-tray') ->button() ->label('Export') ->action(function () { $query=$this->getFilteredTableQuery(); return Excel::download(new PartecipantsExport($query), "PartecipantList - " . Carbon::now()->format("d-m-Y H.i.s").".xlsx"); }), Remember: on $query you can do what you want. i.e. $partecipants=$query->with('contact','exhibition')->get();
Skjena
SkjenaOP14mo ago
Thanks for the tip, however I only want to obtain the current filter value rather than the query, as I would then need to input that into my query builder which is returned by the getTableQuery() method. $this->getFilteredTableQuery() this actually gives me $table must not be accessed before initialization.
ziolupo
ziolupo14mo ago
For the filters I'm using $filters=$this->table->getFilters();
Skjena
SkjenaOP14mo ago
Does this work for the livewire table component? The getTableFilters() function in v2 allows to set the filter fields of a livewire table component. However, in v3, I've noticed that this function has been deprecated. Could you kindly advise me on which function is used in v3 to set the filter fields?
ziolupo
ziolupo14mo ago
in v3 I use directly on the livewire component the following: $this->tableFilters
Skjena
SkjenaOP14mo ago
Thank you, however with some changes The function $this->table->getFilters() returns the value as shown below
array:1 [
"attendance_month" => Filament\Tables\Filters\Filter {#3709 ▶}
]
array:1 [
"attendance_month" => Filament\Tables\Filters\Filter {#3709 ▶}
]
I've tried so many times, but I still can't get the current filter value or state. Could you kindly tell me how to achieve that?
ziolupo
ziolupo14mo ago
Really you don't need that function. If youa are in a page in my case ListPartecipants.php.: dd($this->tableFilter) array:4 [▼ // app\Filament\Resources\PartecipantResource\Pages\ListPartecipants.php:64 "exhibition_id" => array:1 [▼ "value" => null ] "masterclass" => array:1 [▼ "value" => null ] "modulo" => array:1 [▶] "active" => array:1 [▶] ]
Skjena
SkjenaOP14mo ago
Thank you for your response, but I use this filter inside a table widget when dd($this->tableFilters), it returns null.
ZedoX
ZedoX14mo ago
Use the ->query() on Filter?
Filter::make('foo')
->query(fn (Builder $query) => $query->where(...)
Filter::make('foo')
->query(fn (Builder $query) => $query->where(...)
?
Skjena
SkjenaOP14mo ago
Thanks for your response but it won't work in my case because what i am doing is I use makeTable() to initialise the table in the custom table widget. When I dump ($this), two table components are displayed in the output on page refresh. The first output has no table key and tableFilters is null.  there are filters inside the object in table key in the second output, but tableFilters is still null. Because it is only set when I choose a month filter value, I believe this is why the tableFilters always has the previous value. I am able to update the table widget with the table filters value that i received after the filter is updated second time but it returning the previous value. Is there any way to get the current filter value?
Want results from more Discord servers?
Add your server