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
You can access both the query and the filters:
$query=$this->getFilteredTableQuery();
$filters=$this->table->getFilters();
$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
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();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.For the filters I'm using
$filters=$this->table->getFilters();
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?
in v3 I use directly on the livewire component the following:
$this->tableFilters
Thank you, however with some changes The function $this->table->getFilters() returns the value as shown below
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?
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 [▶]
]
Thank you for your response, but I use this filter inside a table widget when dd($this->tableFilters), it returns null.
Use the
->query()
on Filter?
?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?