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

ignaaaam's avatar

How to add Pagination on post grid with filters?

Hello I'm trying to add pagination to a grid of posts that I have where filters get applied from a filter box. Right now works without pagination but i'm trying to add pagination using Livewire Volt (following this https://livewire.laravel.com/docs/volt#pagination) but i'm struggling to know if I should change what I already have to use with.

So what I've been trying is to paginate $this->posts = $query->get(); on the $applyFilters function but i'm not sure how should apply what the doc says (using the with)

This is my grid livewire component without the pagination.

0 likes
1 reply
ignaaaam's avatar
ignaaaam
OP
Best Answer
Level 5

Changed the filtering way to do it using a computed and it's working as it should right now!

In case anyone needs it.

Changed the $applyFilters function into this computed (and removed the mount calling the function)

$filteredPosts = computed(function () {
    $query = Post::query();

    $query->when($this->searchTerm, function ($query) {
        $query->where(function ($query) {
            $query
                ->where('title', 'like', "%{$this->searchTerm}%")
                ->orWhere('description', 'like', "%{$this->searchTerm}%")
                ->orWhere('phone', 'like', "%{$this->searchTerm}%");
        });
    });

    $query->when($this->selectedType, function ($query) {
        $query->where('type_id', $this->selectedType);
    });

    $query->when($this->selectedProvince, function ($query) {
        $query->where('province_id', $this->selectedProvince);
    });

    $query->when($this->selectedTown, function ($query) {
        $query->where('town_id', $this->selectedTown);
    });

    return $query->paginate(25);
});

Please or to participate in this conversation.