Answer the question
In order to leave comments, you need to log in
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
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.
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.
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();
});
public function meta() {
return $this->morphMany('App\Meta', 'post');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question