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

SigalZ's avatar

which relationships to use

Hello,

I want to connect products with different options_values and to be able to query these values with grouping by option.

For example:

//Tables
products:
id
name

options:
id
name

option_values:
id
option_id
value

//Data
options:
1 - Colors
2 - Sizes

option_values:
1 - 1 - Red
2 - 1 - Green
3 - 1 - Blue
4 - 1 - Yellow
5 - 2 - Big
6 - 2 - Small

product_option_values:
product_id: 1
option_value_id: 2

product_id: 1
option_value_id: 3

product_id: 1
option_id_value: 5

Is there a way to display the product's values grouped by the option_id?
e.g.:
Color:
Green, Blue

Size:
Big

What relationship should I use in the models?

Or should I created different tables structure? something like:

product_options:

id, product_id, option_id

product_option_values:

product_option_id, value

A bit confused.

Thank you

0 likes
4 replies
SigalZ's avatar

@vincent15000 Thank you, but what I don't like in this design is that they have hardcoded attributes, color and size, what if a product has more attributes than that? it is not dynamic.

1 like
SigalZ's avatar

@martinbean Thank you. Your db design is almost like mine, the difference is you call it sku and I call it variants.

The issue is, I want to have general options/attributes tablet that will hold values like color, size etc.

These options will have values.

So the user can select an option for the product and automatically have all the option values instead of inserting them for each product, and he can also remove some values or add custom option values for each product.

But for display, I want to group all the product values by the option and that's where I get stuck.

I wonder if I should use hasManyThrough somehow, but can't get my head around what to do.

After having some coffee, I think I got the simple point I was missing... Just run on product_options and get the option_id from the option from there.

Gonna play with this, will let you know...

1 like

Please or to participate in this conversation.