Tricks

Getting the table's query with sorting and filtering applied

Jul 3, 2022
Andréia Bohner
Admin panel, Table builder

You can use the getFilteredTableQuery() and applySortingToTableQuery($query) methods to get the table query with the selected filtering and sorting applied.

The example below is an action to export the table using the Laravel Excel library:

<?php
 
namespace App\Filament\Resources\UserResource\Pages;
 
use App\Exports\UsersExport;
use Filament\Resources\Pages\ListRecords;
use Maatwebsite\Excel\Facades\Excel;
// ...
 
class ListUsers extends ListRecords
{
// ...
 
public function export()
{
$query = $this->getFilteredTableQuery();
$this->applySortingToTableQuery($query);
 
$users = $query->get();
 
$filename = now()->format('Y-m-d_his').'-users.xlsx';
 
return Excel::download(new UsersExport($users), $filename);
}
 
// ...
}
avatar

Could you please explain how I use this code in the resource