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:Jump to 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. ...
5 Replies
Hey @Jim ! Have a look here => https://filamentphp.com/docs/3.x/panels/resources/getting-started#customizing-the-resource-eloquent-query:~:text=or%20an%20ID.-,%23,-Customizing%20the%20resource
You can do a select operation to specify your columns and return the eloquent query.
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
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
@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
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.