S
S
Sergey Khlopov2019-08-29 16:21:50
Laravel
Sergey Khlopov, 2019-08-29 16:21:50

If you make several models for pictures?

Hello, tell me please, I have a category model Categoryand pictures Image, and the corresponding tables in the database for these models. A category can have only one picture, and I thought to make a relationship between Categoryand Image"One to One", but I will also have a product model Product, products can have many pictures, respectively, if only one model table is usedImageboth for categories and for products, then somehow it doesn’t work out right. And what is the best thing to do then? I thought I could make a separate image model for categories and a separate model for products, but then what about methods common to images, for example, methods: saving an image, deleting, etc. And if you use the "Many-to-many" relationship, then perhaps the problem in the sampling speed appears in the number of tables as well. then it is necessary to create intermediate tables.
What is the best thing to do in such a situation? Thank you in advance for your response.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2019-08-29
@JhaoDa

Start reading the documentation, in particular, about polymorphic relationships.

Possibly a speed issue
Premature optimizations are premature.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question