shimana's avatar

resetPage not work in livewire

i have a query , I tried two methods for pagination. The page reset works in the first method but does not work in the second method.

methods one:

    public function render()
    {
        $images = Gallery::query()
            ->when($this->selected, function($query) {
                return $query->where('status', $this->selected);
            })->paginate($this->perPage);
        return view('livewire.dashboard.add-image', compact('images'));
    }

I will reset the page using the this function.

public function updatedSelected(){
    $this->resetPage();
}

in blade:

{{ $images->links() }}

So far everything is fine and it resets well.

But when I want to use the Infinite Scrolling method The query works but the page does not reset.

method 2:

class ImageList extends Component
{

    use WithPagination;

    public $perPage = 10;
    public function loadMore()
    {
        $this->perPage += 10;
    }
    public function render()
    {
        $images = Gallery::query()
            ->when($this->selected, function($query) {
                return $query->where('status', $this->selected);
            })->paginate($this->perPage);
        return view('livewire.dashboard.image-list', compact('images'));
}

in blade:

 @if($images->hasMorePages())
        <button wire:click.prevent="loadMore">Load more</button>
    @endif

By changing the category of the page can not be reset in the second method. Why does in the second method page reset not work? please guide me ,Thanks.

0 likes
2 replies
shimana's avatar

I've noticed that a resetPage() may not work on a single page But is there really no solution?

Snapey's avatar

don't use pagination for infinite scrolling. have a row count that you increment and use it with take()

1 like

Please or to participate in this conversation.