M
M
Misty Hedgehog2017-01-25 09:20:26
Laravel
Misty Hedgehog, 2017-01-25 09:20:26

Model hierarchy in Laravel?

Good day, %username%!
Look what a thing. The answer to the question described below probably lies on the surface, but something cannot be formulated in any way.
We have an abstract factory that produces products. Absolutely all products have such a property as an article . In addition, all products are divided into two classes :

  • doors
    1. Type: Interior doors ( properties : material, width, height, color)
    2. Type: Entrance doors ( properties : material, width, height, fire resistance)

  • Window
    1. Type: Plastic windows ( properties : material, width, height, color)
    2. Type: Wooden windows ( properties : material, width, height, varnished)


In order to describe these relationships in Laravel, we can create one migration of the product table, describe in it all the possible properties that are mentioned in the types (class, type, article, width, height, material, whether it is fireproof, whether it is varnished), after why create models of the necessary products, preserving the inheritance we need, but the question is how, for example, to specify for the model, when working with a common table (where all products are stored in general), return only those that have, for example, class = (int) 2?
Moreover, if I need to return all windows - I just want to pull the /App/Models/Products/Doors model and get a collection of all doors in general, and if I pull /App/Models/Products/Doors/Interior - then only interior doors.
In general, what is the best way to organize eloquent in this case? For each product, create its own table, but then how to include it in all "parent" models?
Or if you store all products in one, and when adding a new product (with a new property, for example) - just write a migration to add a field? But how then to force the "parent" models to return the newly added ones, and the specific model of the added product - only it?
Or is it better to put key properties such as class and type into separate tables and link them to hasMany/hasOne?
Tell me please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Finsh, 2017-01-25
@paramtamtam

In order to describe these relationships in Laravel, we can create one migration of the product table, describe in it all the possible properties that are mentioned in the types (class, type, article, width, height, material, whether it is fireproof, whether it is varnished), after why create models of the necessary products, preserving the inheritance we need, but the question is how, for example, to specify for the model, when working with a common table (where all products are stored in general), return only those that have, for example, class = (int) 2?

You are going to store quite a lot of extra, empty cells and create a lot of unnecessary models. Don't do it. Products are not divided into 2 classes, but into 2 types. And the type (plastic windows, wooden windows) is one of the properties of the product (material).
1. One table for products, where you store the article and required fields for all products.
2. One table for product properties where you store all product properties.
3. A many-to-many relationship will tell which product which property belongs to. This will allow the flexibility to add properties and attach them to products without the intervention of a programmer.
4. One table - one model. You can get confused with both DM and repositories, etc. but if on simple, then one table - one model. Link them with links.

O
OnYourLips, 2017-01-25
@OnYourLips

Product (nested set), Attribute (refer to different product levels), Value

K
Kirill Nesmeyanov, 2017-01-25
@SerafimArts

I think it's worth adapting the ECS pattern, and more specifically Entity + Component. I think it will fit perfectly.
PS Initially, it was developed for gamedev, where there is an archer, an archer-mage, an archer-mage-with-a-spear, etc. At the same time, there are no restrictions on the characteristics and their relationships.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question