How to Dynamically Filter and Export Table Data in Filament

When building applications with Filament, you often need to generate reports based on the records displayed in a resource table. However, ensuring that only the currently filtered, sorted, and searched records are included in the report can be tricky. Fortunately, Filament provides a powerful method to retrieve precisely those records: getFilteredSortedTableQuery().
Understanding getFilteredSortedTableQuery()
The getFilteredSortedTableQuery() method is part of Filament's HasRecords trait, which is commonly used in Table components. This method returns a Laravel Builder instance that includes all applied filters, search parameters, and sorting configurations. This makes it ideal for extracting only the relevant records when performing bulk actions or generating reports.
Using getFilteredSortedTableQuery() in Filament Actions
A common use case for this method is when generating reports from a table's filtered records. Below is an example of how you can utilize this method inside a Filament action:
->action(function (array $data, $livewire) { // Retrieve the query with applied filters, sorting, and search $query = $livewire->getFilteredSortedTableQuery(); // Clone the query to avoid modifying the original $query = clone $query; // Generate the report using the filtered query results return static::generateBulkRequisitionReport($data, $query); })
Why Clone the Query?
Cloning the query is a best practice to ensure that the original query instance inside Filament remains unaffected. Without cloning, modifying the query in subsequent operations could unintentionally alter the data displayed in the Filament table.
Practical Example: Generating a PDF Report
If you are generating a PDF report, you might pass the query results to a PDF generator like DomPDF:
public static function generateBulkRequisitionReport($data, $query) { $records = $query->get(); // Generate PDF from filtered records $pdf = PDF::loadView('reports.requisition', compact('records')); return response()->streamDownload(fn () => print($pdf->output()), 'Requisition_Report.pdf'); }
Benefits of Using getFilteredSortedTableQuery()
- Dynamic Filtering: The report only includes the records visible after applying table filters.
- Consistent Data: Sorting and search terms applied in the UI reflect in the generated report.
- Performance Optimization: No need to refetch or reprocess data—work with the already optimized query.
Conclusion
Using getFilteredSortedTableQuery() allows you to seamlessly integrate Filament tables with custom reporting actions, ensuring that generated reports accurately reflect the user’s current view. Whether you're exporting data, creating summaries, or generating PDFs, this method provides a robust and efficient way to retrieve table data without additional query manipulation.
By leveraging this approach, you enhance the user experience while maintaining data integrity and performance within your Filament-powered application. 🚀
Limited-time offer - $350/Week
Turn your vision into reality. Our subscription service offers continuous Laravel Filament development and support. Start building your future today!
Schedule a CallRelated Articles that May Be to Your Interest

Build a Modern Admin Dashboard for Your Web Applications
Revamp your web app's admin interface with a sleek, modern dashboard design. Enhance user experience and streamline functionality effortlessly.

Laravel Filament: How to Properly Sort by Pivot Table Columns
Learn how to correctly sort related records in Filament without MySQL errors by referencing the correct table in defaultSort

Generating URL Modal Pop-Ups within FilamentPHP Resource Pages
Learn to set up FilamentPHP, create a resource, define a modal actions, and how to generate urls to those actions from any part of your application