F
Filament2mo ago
Prodex

Overriding paginateTableQuery doesn't work on Widgets

The TableWidet Class has a paginateTableQuery method where simplePagination is used. The docs say, this can be overwritten. But using the method in my Widget doesn't work. It still shows the simplePagination. With dd() I can verify that the method gets called though. Any ideas? Code:
use Filament\Widgets\TableWidget;
use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class CompanyProximitySearch extends TableWidget
{
public ?Model $record = null;

protected int|string|array $columnSpan = 'full';

protected function paginateTableQuery(Builder $query): CursorPaginator
{
return $query->cursorPaginate(($this->getTableRecordsPerPage() === 'all') ? $query->count() : $this->getTableRecordsPerPage());
}
}
use Filament\Widgets\TableWidget;
use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class CompanyProximitySearch extends TableWidget
{
public ?Model $record = null;

protected int|string|array $columnSpan = 'full';

protected function paginateTableQuery(Builder $query): CursorPaginator
{
return $query->cursorPaginate(($this->getTableRecordsPerPage() === 'all') ? $query->count() : $this->getTableRecordsPerPage());
}
}
Solution:
Yes I see, but in that case the paginateTableQuery is set to simple when extending the TableWidget. Changing it to extend "Widget" solves the issue. But I still don't know why the overwrite didn't work, as it normally should.
Jump to solution
6 Replies
LeandroFerreira
LeandroFerreira2mo ago
add this trait
use \Filament\Tables\Concerns\CanPaginateRecords;
use \Filament\Tables\Concerns\CanPaginateRecords;
Prodex
ProdexOP2mo ago
Hm... I did that now, but it's still a simple Pagination in the Frontend.
use App\Filament\Resources\CompanyResource;
use App\Models\Company;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\CanPaginateRecords;
use Filament\Tables\Enums\FiltersLayout;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget;
use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class CompanyProximitySearch extends TableWidget
{
use CanPaginateRecords;

public ?Model $record = null;

protected int|string|array $columnSpan = 'full';

protected function paginateTableQuery(Builder $query): CursorPaginator
{
return $query->cursorPaginate(($this->getTableRecordsPerPage() === 'all') ? $query->count() : $this->getTableRecordsPerPage());
}
use App\Filament\Resources\CompanyResource;
use App\Models\Company;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\CanPaginateRecords;
use Filament\Tables\Enums\FiltersLayout;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget;
use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class CompanyProximitySearch extends TableWidget
{
use CanPaginateRecords;

public ?Model $record = null;

protected int|string|array $columnSpan = 'full';

protected function paginateTableQuery(Builder $query): CursorPaginator
{
return $query->cursorPaginate(($this->getTableRecordsPerPage() === 'all') ? $query->count() : $this->getTableRecordsPerPage());
}
LeandroFerreira
LeandroFerreira2mo ago
remove paginateTableQuery method. Simple pagination will also automatically adjust based on screen size, utilizing Tailwind's responsive breakpoints.
Solution
Prodex
Prodex2mo ago
Yes I see, but in that case the paginateTableQuery is set to simple when extending the TableWidget. Changing it to extend "Widget" solves the issue. But I still don't know why the overwrite didn't work, as it normally should.
LeandroFerreira
LeandroFerreira2mo ago
I didn't understand your question and didn't also understand why you marked your answer as answered 🤷‍♂️
Prodex
ProdexOP2mo ago
Sorry, let me explain: My Widget extended "TableWidget", which has the following vendor code:
protected function paginateTableQuery(Builder $query): Paginator | CursorPaginator
{
return $query->simplePaginate(
perPage: ($this->getTableRecordsPerPage() === 'all') ? $query->count() : $this->getTableRecordsPerPage(),
pageName: $this->getTablePaginationPageName(),
);
}
protected function paginateTableQuery(Builder $query): Paginator | CursorPaginator
{
return $query->simplePaginate(
perPage: ($this->getTableRecordsPerPage() === 'all') ? $query->count() : $this->getTableRecordsPerPage(),
pageName: $this->getTablePaginationPageName(),
);
}
So using TableWidget the pagination is already set to simple. Which I should be able to overwrite but that didn't work for some reason. Now, since you can also use custom widgets which just extend "Widget" that don't set the pagination to simple, I implemented HasTables and HasForms myself and the cursor pagination appeared as expected. I just don't understand why overwriting the paginateTableQuery method while extending TableWidget didn't do anything. For your last response: just removing "paginateTableQuery" in my Widget would still cause the pagination to be simple, as it is set so in the TableWidget. Did you understand that? 😅

Did you find this page helpful?