F
Filament16mo ago
.jw.

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
.jw.
.jw.OP16mo 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 S16mo 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 Kidd16mo 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
.jw.
.jw.16mo 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( ... );
}
})
Want results from more Discord servers?
Add your server