How to filter posts by type, province, town and search term with pagination on Livewire Volt

Hello I’m making a project where I have a sub-header where you can apply filters for posts and this filters go to a grid parent component where the posts appear. Right now it’s working what I have but I would like to know if i’m doing it well since now i’m trying to add Pagination and it’s giving me a headache to add the Pagination with what I have already built.

I just want to know if this is the correct way and i’m in the good path since I didn’t find another way. Right now, I have the filters of type, province, town and search term which are in a child component of sub-header. This filters goes to the parent component (grid) using dispatch and only the search term applies when using the submit button, the rest of filters apply instantly.

I’m using an $applyFilters function to update the posts depending on the filters on the grid but this way of using this function is driving me crazy when I try to add Pagination to the posts.

Am I doing it good? Or should I approach this another way to be able to add pagination later?

<?php

use function LivewireVolt{state, uses, on, mount, computed};

use AppModelsPost;

state([
    'posts' => collect(),
]);

state([
    'searchTerm' => '',
])->url(as: 'search');
state(['selectedProvince'])->url(as: 'province');
state(['selectedTown'])->url(as: 'town');
state(['selectedType'])->url(as: 'type');

mount(function () {
    $this->applyFilters();
});

$applyFilters = 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);
    });

    $this->posts = $query->get();
};

on([
    'type-selected' => function ($typeId) {
        $this->selectedType = $typeId;
        $this->applyFilters();
    },
]);

on([
    'province-selected' => function ($provinceId) {
        if ($this->selectedProvince !== $provinceId) {
            $this->selectedTown = null;
            $this->searchTerm = '';
        }
        $this->selectedProvince = $provinceId;
        $this->applyFilters();
    },
]);

on([
    'town-selected' => function ($townId) {
        $this->selectedTown = $townId;
        $this->applyFilters();
    },
]);

on([
    'post-searched' => function ($searchTerm) {
        $this->searchTerm = $searchTerm;
        $this->applyFilters();
    },
]);

?>

<div class="block">
    <div class="flex-grow bg-primary-black-darker/50 p-4 mx-auto w-full rounded-md mb-8">
        <livewire:sub-header :selectedType="$selectedType"
                             :selectedProvince="$selectedProvince"
                             :selectedTown="$selectedTown"
                             :posts="$posts" />
    </div>
    <div class="bg-primary-black-darker/50 p-4 container w-full rounded-lg grid grid-cols-2 gap-4">
        @forelse ($this->posts as $post)
        <a class="relative flex bg-primary-black-darker/50 border border-gray-500/10 p-4 rounded-md mb-4 w-full hover:border-gray-500/30 transition hover:-translate-y-1 hover:bg-primary-black-darker/40"
           href="{{ route('posts.show', $post->slug) }}"
           wire:navigate>
            <div class="p-4 absolute -top-8 right-0">
                <p
                   class="uppercase text-[10px] rounded-md font-semibold w-full border py-1 px-2 bg-primary-red text-primary-white">
                    {{ $post->type->name }}
                </p>
            </div>
            <div class="w-[400px] items-center justify-center">
                <img src="{{ $post->main_image->image_path }}"
                     alt="{{ $post->title }}"
                     class="w-[200px] h-48 object-cover rounded-md">
            </div>
            <div class="flex flex-col justify-between px-2 pb-8">
                <h3 class="text-pretty text-lg font-bold justify-self-start tracking-wide">
                    {{ $post->title }}</h3>
                <p class="text-pretty text-base line-clamp-3 tracking-normal">{{ $post->description }}</p>
            </div>
            <div class="p-4 mt-8 absolute bottom-0 right-0">
                <p class="uppercase text-xs font-semibold w-full opacity-40">
                    {{ $post->created_at->diffForHumans() }}
                </p>
            </div>
        </a>
        @empty
        <div class="col-span-2 flex items-center justify-center text-primary-white/70 text-center">
            No se han encontrado posts
        </div>
        @endforelse
    </div>
</div>

Right now this is working but without pagination and I was wondering if I’m doing it well since when I try to add the it’s driving me crazy. Maybe i’m doing something wrong that needs to be changed before adding pagination? If you need the child component to see where the filters come from I will share it right away.

Remember i’m using Laravel Livewire with Volt package.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật