Answer the question
In order to leave comments, you need to log in
How to implement and store inherited objects/models?
I am currently reading literature on design patterns. And an example is considered:
There is a Product class, BookProduct and CdProduct are inherited from it and they differ in some methods and properties.
BookProduct has numPages properties and a getNumberOfPages method, while CdProduct has playLenght and getPlayLenght.
And I can’t figure out how to implement this within the framework of Eloquent. Is this Product model supposed to be or is there still 2 BookProduct and CdProduct models, so then these are 2 tables?
And if there are many types of products, is it necessary to create a table for each type? On the other hand, if there is one table, then there will be too many columns. And if inheritance from BookProduct and CdProduct starts further?
In general, on paper, everything looks good and beautiful, but as I want to dig deeper into Laravel in practice, it’s a stupor.
Answer the question
In order to leave comments, you need to log in
I do this
in the beginning, I inherit the MyProduct class from the base Model in which the general logic is implemented
and BookProduct CdProduct is already inherited from it, respectively, no MyProduct objects ever exist, there is no table for it,
or the general logic can be made a trait, but it seems to me more correct to do inheritance here
I would do this:
Tables: cds, books, products. I would add 1k1 connections to them.
According to the models: Cd, Book, Product. Cd and Book have a belongsTo relationship to Product
Why inherit something? You just need to implement additional fields for products, see how all stores are written, they allow you to create any products with any additional options.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question