Livewire pagination does not reset page after query filter changes
0 reputation · 19 Aug 2021, 22:14 UTC
0 reputation · 19 Aug 2021, 22:14 UTC
A Livewire component using the WithPagination trait must maintain the current page number when the underlying data query is altered by filters or other parameters.
The component re‑renders on each pagination request, and the paginator is bound to the query string, yet there is no automatic reset of the page index when the result set size changes, potentially showing an empty page or stale links.
Developers must decide whether to manually invoke $this->resetPage() or implement a custom reset logic, but the framework does not provide a built‑in mechanism for this scenario.
How can Livewire be extended to automatically reset the page when the query changes? Is there a supported event or hook for this behavior? What is the recommended pattern for handling filter‑driven pagination resets?
35775 reputation · 20 Aug 2021, 08:07 UTC
Livewire’s WithPagination trait does not automatically reset the page index when a filter property changes. The framework provides no built‑in event or hook that triggers a reset solely because the underlying query result set shrinks or grows. The supported pattern is to call $this->resetPage() inside the property‑updated hook that corresponds to each filter (e.g. updatedSearch(), updatedStatus()).
$page property (Livewire 2) or a #[Url]‑backed property (Livewire 3).$page value is preserved, so the same page number is sent to the paginator.updated{Property}() method (Livewire 2) or a #[On('updated:{property}')] listener (Livewire 3) that calls $this->resetPage();.resetPage() inside render(); that would reset the page on every pagination click.// Livewire 2 example
class UserTable extends Component
{
use WithPagination;
public $search = '';
public $status = '';
public function updatedSearch()
{
$this->resetPage();
}
public function updatedStatus()
{
$this->resetPage();
}
public function render()
{
return view('livewire.user-table', [
'users' => User::query()
->when($this->search, fn($q) => $q->where('name', 'like', "%{$this->search}%"))
->when($this->status, fn($q) => $q->where('status', $this->status))
->paginate(15),
]);
}
}
If you have many filter properties and want a single place to reset the page, create a small trait that observes all public properties ending with Filter (or any naming convention) and calls resetPage() in a generic updated method. This is a user‑land solution; Livewire itself does not ship such a hook.
composer show livewire/livewire to confirm the major version (2.x or 3.x).page query parameter should become 1 after the filter update.Are you using query‑string pagination ($queryString in Livewire 2 or #[Url] in Livewire 3) and do you need the page parameter to stay in the URL after a filter reset? If you intentionally want the page number to survive a filter change (e.g. for deep linking), you would omit resetPage() for that specific filter.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 20 Aug 2021, 03:43 UTC
One version-specific clarification: Livewire 3 supports a generic updated($property) hook, while Livewire 2 code often uses property-specific methods such as updatedSearch(). Do not assume an #[On('updated:...')] listener is equivalent to that lifecycle hook; verify the installed package’s behavior against its documented version.
Also avoid treating the paginator’s own page-state update as a filter change. When pagination is URL-bound, verify two outcomes separately: the component renders page 1 and the browser URL reflects that state. With wire:model.live.debounce, the reset occurs after the debounce interval, not on every keystroke.