In this episode, we really get into making our product filtering system work smoothly with MeliSearch. We start by making sure our filter setup (things like color and size) is correctly captured from the UI using Livewire. Each time you click a filter, a request is fired, and our filters are updated and applied to the product query.
Next, we dive into how we map and format these filters into a string that MeliSearch understands. This involves filtering out any empty selections, handling multiple selected values (like multiple colors), and then building the final filter string. We even add a custom recursive collection macro in Laravel to help tackle nested arrays and make our code more versatile.
There’s a bit of trial and error (complete with lots of dd()
die-dump debugging), as we refine the formatting and filtering logic. By the end, we've set things up so that when a user selects different product filters, only the relevant products are shown—just like you'd expect from any solid e-commerce shop. The facet distribution (those little counts by each filter) also updates as you filter more and more.
It’s a little complex, but you can now filter products in real-time—laying the groundwork for adding even fancier features (like filtering by price next).