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

r123's avatar
Level 3

Storing product specifications in database

I can't think of a good way to store product specifications in database so that later I can display them in a product view.

e.g., I have a computer different components stored in products table - motherboard, CPU, GPU, memory, case, etc.

They can have:

Brand, manufacturer, size, clock speed, memory clock speed, ports, etc.

I want to display those specifications for a product like this:

  • Brand: Asus

  • Model: STRIX-RX470-O4G

  • Chipset Manufacturer: AMD

  • ...

In what way should I store this information about the product so that I can display it on a page later?

0 likes
4 replies
Organizm238's avatar
Level 8

You have 3 options depending on your needs and project size.

I. Keep it all in 'products' table:

Just add specifications field into products table if the quantity on fields in not so high. It is easier for acessing and filtering but harder for extenion and modification.

Access:

$product->brand;

Filtering:

Product::where('brand', 'Asus')->get();

II. Use one-to-one relation with 'product_specifications' table:

Table 'product_specifications' with all those fields (id, product_id, brand, model, chipset_manufacturer etc). Write a relation method in Product class to access specification:

public function specification() {
    return hasOne(ProductSpecification::class);
}

Acess:

$product->specification->brand;

Filtering:

$productsWithAsusBrand= Product::whereHas('specification', function($query) {
                $query->where('brand', 'Asus');
            })->get();

III. Use many-to-many relation with 'specification_types':

Tables:

specification_types(id, name)

product_specifications(id, product_id, type_id, value)

product(id, name, etc)

So your specification_types will store all the specifications type like rows:

id name
-----------
1 Brand
2 Model
...

And your pivot product_specifications will store all specifications values:

id product_id type_id value
----------------------------------
1   1           1        Asus

In your Product model:

public function specificationTypes() {
    return belongsToMany(SpecificationType::class, 'product_specifications');
}

In your SpecificationType model:

public function products() {
    return belongsToMany(Product::class, 'product_specifications');
}

Acess:

foreach ($product->specificationTypes as $type) {
    var_dump($type->name . ': ' . $type->pivot->value)
}
2 likes
frezno's avatar

imho you have to have a products table, an attributes table and a pivot table.

The products table contains the basic product, the attributes table all the components, the user can select of and the pivot (lookup) table, which connects both.

kabircse's avatar

What happens , when there have product variation ? Ex:

Product: id(1)->name(t-shirt)->category(1)->etc

Stock: product_id(1)->color(green)->size(xl)->qty(10)->price(250)->stock_status(in stock)

There may other attribute like as color,size.

pwnz22's avatar

What if i my specifications has a parent and i want to render them in product by looping from parents?

Please or to participate in this conversation.