A
A
Anton Seredny2019-06-11 14:18:02
MySQL
Anton Seredny, 2019-06-11 14:18:02

How to store "product" attributes in laravel if there are common and unique attributes?

I have an entity - PRODUCT
The goods themselves are divided into subgroups. For example: spare parts, tires, batteries.
All products have common fields:
-name
-category
-date of publication
...
And there are unique ones, inherent only in some categories of products
-radius
-tread height, etc.
How to store "product" attributes in such a case?
I was thinking of making a table with attribute names and linking them to product categories + a table with attribute values ​​linked to the product itself.
But what about common attributes and what about attributes that can be shared by several product categories at once?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Romashkan, 2019-06-11
@EvgeniiR

In general, what you did is called EAV(Entity, Attribute, Value), you can do it more beautifully and faster using json - https://coussej.github.io/2016/01/14/Replacing-EAV...
Due to common fields, problems are unlikely to arise, it is too early to optimize.

K
Konstantin, 2019-06-11
@potkot

I would do (and did) like this:
Common fields directly in the table Goods
Attributes (characteristics): Table of characteristics, Table of binding a characteristic to a category, Table of characteristic values ​​for a product.

P
pLavrenov, 2019-06-15
@pLavrenov

There is a common implementation of meta fields for this.
This is the field table (example)

Schema::create('metas', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('key')->index();
            $table->text('value')->nullable();
            $table->uuid('post_id');
            $table->text('post_type');
            $table->timestamps();
        });

The model exhibits polymorphic relations:
public function meta() {
        return $this->morphMany('App\Meta', 'post');
    }

Polymorphic are made so that the fields can be used for any models.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question