Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shimana's avatar

pagination in a query

I want to filter the display of images with this query. And it works for me but the problem is with the pagination. I use Load More On Page Scroll. When I display images in a category and click load more, more images are displayed. But when I go to another category, like the previous category, more images are displayed without me clicking on load more. I want more images to be displayed after entering each category, by clicking on load more. thank you for your help.

    public function render()
    {
        $images = Gallery::query()
            ->when($this->selected, function ($query, $selected) {
                $query->when($selected == '1', function ($query) {
                    $query->where('user_id', auth()->id());
                },function ($query) use ($selected) {
                        $query->where('category', $selected);
                    });
            }, function ($query) {
                $query->where('user_id', 1);
            })->orderByDesc('created_at')->paginate($this->perPage);
        return view('livewire.dashboard.add-image', compact('images'));
    }
0 likes
5 replies
tykus's avatar

when I go to another category, like the previous category, more images are displayed without me clicking on load more

What do you do; update the perPage variable whenever Load More is clicked? Just reset it whenever $selected is updated using the updatedSelected method (assuming Livewire?)

Your work would be more efficient if you appended the next page rather than changing the perPage variable (assuming that is what you're doing)!

1 like
undeportedmexican's avatar

I'm guessing this is Livewire?

If that's the case, you render() function will be called every time you update any property, this includes your selected category.

You can add the :defer modifier to the wire:model where you're setting the category.

1 like
undeportedmexican's avatar

@shimana What defer loading does, it that it doesn't make any requests to the server automátically, but rather waits until the next request to send the payload. Did you apply this only to the category model?

When you click the load more button, what are you doing? That should trigger a component update and should send the deferred data to the server.

Please or to participate in this conversation.