In this episode, we tackle the missing link between products and categories in our app. Previously, when you clicked on a category like "Nike," it didn't actually show any products associated with that category. So, we set out to fix that!
Here's what we do:
category_product
to establish the relationship between products and categories, following Laravel's conventions.belongsToMany
relationships in both the Category
and Product
models. This allows us to easily fetch all products for a category and all categories for a product.toSearchableArray
method to include category IDs, so when we're searching for products under a specific category, it all just works.In the end, we confirm that products are correctly associated with categories by looking them up in our search index. Now, when we build our product browser, we'll be able to pull and display products by category without any headaches. Everything's ready for the next step—scaffolding out our actual product browser UI in the upcoming episode!