F
Filament2y ago
jim

Conditionally modify query based on column visibility

Does anyone know a way to conditionally modify a Table query based on the visibility of a column? I've got a couple of columns that are based on withSum, and I'd rather not have them as part of the base query if they aren't being shown.
Solution:
Ahhh...thanks @K R A T O S and @Brian Kidd that put me on the right path...this works perfectly for what I want it to do.
->modifyQueryUsing(function (Builder $query, Table $table): void {
if($table->getColumns()['expensive_column']->isVisible()){
$query->withSum( ... );
}
})
->modifyQueryUsing(function (Builder $query, Table $table): void {
if($table->getColumns()['expensive_column']->isVisible()){
$query->withSum( ... );
}
})
...
Jump to solution
5 Replies
jim
jimOP2y ago
Thanks @K R A T O S , I think I've got that part. What I'm unsure of is how to do that based on the visibility of a column. What I feel like I'm looking for is doing something like this when defining whether the column can be toggled
->toggleable(true, true, query: function (Builder $query): Builder {
return $query->withSum(...);}),
->toggleable(true, true, query: function (Builder $query): Builder {
return $query->withSum(...);}),
K R A T O S
K R A T O S2y ago
Ah. I Understand what you wanna do now. Maybe you need to modify the query as and when you toggle the columns.... What I mean to say is have a very primal query or a base query with the getEloquentQuery() function and then when the columns are toggled, maybe change query for that particular field. look here to inject table => https://filamentphp.com/docs/3.x/tables/columns/advanced#:~:text=%23-,Injecting%20the%20current%20table%20instance,-If%20you%20wish and then modify query for table => https://filamentphp.com/docs/3.x/panels/resources/listing-records#customizing-the-table-eloquent-query:~:text=%23-,Customizing%20the%20table%20Eloquent%20query,-Although%20you%20can
Brian Kidd
Brian Kidd2y ago
@Jim you should be able to inject the Livewire component (HasTable, see docs) and then check the column toggle inside your modifyQueryUsing. Hope that makes sense. Haven’t tried it but should be feasible.
Solution
jim
jim2y ago
Ahhh...thanks @K R A T O S and @Brian Kidd that put me on the right path...this works perfectly for what I want it to do.
->modifyQueryUsing(function (Builder $query, Table $table): void {
if($table->getColumns()['expensive_column']->isVisible()){
$query->withSum( ... );
}
})
->modifyQueryUsing(function (Builder $query, Table $table): void {
if($table->getColumns()['expensive_column']->isVisible()){
$query->withSum( ... );
}
})

Did you find this page helpful?