CrastyCrap's avatar

Get All Categories

i have 10k products and each one have multi categories recorded in database and when user enter to category i want to add option to make user able to filter product by categories the problem is i have to use pagination of course to mange this amount of records so i cant get product categories by relation so what is the best solution

0 likes
8 replies
LaryAI's avatar
Level 58

One solution could be to use a separate table to store the product categories and their associations with the products. This table could have columns for the product ID and category ID, allowing for efficient querying and filtering.

To populate this table, you could write a script that loops through all the products and their categories, and inserts the appropriate records into the new table.

Then, when a user selects a category to filter by, you can query the new table to get all the product IDs associated with that category, and use those IDs to retrieve the relevant products from the main products table.

Here's an example of how you could structure the new table:

CREATE TABLE product_categories (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    product_id INT UNSIGNED NOT NULL,
    category_id INT UNSIGNED NOT NULL,
    INDEX product_category_index (product_id, category_id)
);

And here's an example of how you could query the table to get all the product IDs associated with a given category:

SELECT product_id FROM product_categories WHERE category_id = :category_id;
Snapey's avatar

Not sure where the problem is?

User clicks on a category (eg category_id = 5)

You send them to a route that accepts the category ID

return paginated list of products with category_id = 5

CrastyCrap's avatar

@Snapey no, that's not what i mean in amazon you can search by category as example you searched by laptop you will get 10k product as example and you will find filter by category in sidebar in this filter you will find all categories this 10k are using that's what i want to get the categories of that 10k or what is the number of the records so the user can filter the products with them

ranto's avatar

@Snapey I think the goal is for a set of products, get all of the categories in that set so he can build something like a category filter. Like that amazon example.

If that's the goal, it will depend on how is @crastycrap handling the categories inside the products.

BurtDS's avatar

I think you’ll need to ad a hasManyThrough in you Category Model. By doing so you can get all the related categories of the products in your category. Once you have that you can throw distinct() after that and after that apply your pagination.

Is that what you’re aiming for?

CrastyCrap's avatar

@BurtDS Not exactly, your approach will work if I am showing only one category, but that's not my case.

my case: the user search by keyword let's say t-shirt so what I am doing is filtering products by name and paginating them let's say I have 4 pages the products in the first 2 pages all of them related to category 1 and 2 and the others related to category 3 and 4 is that good? so i want the user to be able to filter by those categories so in the sidebar category 1 and 2 and 3 and 4 will be shown there so he can choose between them if he filters by category 3 as an example products the 3 and 4 page appear and so on

Please or to participate in this conversation.