Query only toggled/displayed columns

I have a table with 100+ columns and for performance reasons the table displays only limited amount of them using toggleable( . However, all columns are selected from DB.

First I thought that it's a bug, why wouldn't Filament select only the columns that are defined on table but then realised that developer can set state using full record.

So I'm looking for a way to manually set the query with select( but for that I need to access toggled columns. Is there a way to do that?
Solution
The query need to be set as closure
$table->query(function () {
   // obtain $query 
   // get $selectableColumns from script above
   return $query->select($selectableColumns)->addSelect('id');
})

because session is updated only once the table is initialized. So when user checks new column to toggle, it's not yet in the session. Using closure fixes that.
Was this page helpful?