Answer the question
In order to leave comments, you need to log in
Is there a problem in choosing a relation or is the database structure not correct?
During the week, from day to day, I rewrite everything anew. Stuck in one place due to the fact that it is not possible to design the database correctly. It turns out that everything seems to be correct, but when it comes to completion, I run into the wrong structure.
There are 3 tables:
Manufacturer.
- Has id
and name
.
Collection.
— Has id
, manufacturer_id
and name
.
Colour.
— Has id
, collection_id
and name
.
Logic:
One manufacturer can have many collections. One collection can belong to one manufacturer. A collection can have many colors. One color can only belong to one collection.
I must say right away that color is not a color in the usual sense, but a physical thing. If it were a color in the usual sense, then it is logical that one color can belong to different collections, but this is not the case. In our case, color is what we use as a name for an unnamed item. Collections are not a collection in the Laravel sense, but simply the name of a table and a model.
— 1. Производитель.
— — 1. Коллекция.
— — — 1. Цвет.
— — 2. Коллекции.
— — — 2. Цвет.
— 2. Производитель.
— — 3. Коллекция.
— — — 3. Цвет.
— — 4. Коллекция.
— — — 4. Цвет.
hasMany()
(one-to-many), but it turned out to be a little more complicated. (Страница производителя)
Производитель — Коллекции — Цвета
Имя_____________2(две)______2(два)
(Страница коллекции)
Производитель — Коллекции — Цвета
Имя_____________Имя_________1(один)
Имя_____________Имя_________1(один)
(Страница цвета)
Производитель — Коллекции — Цвета
Имя_____________Имя_________Имя
Имя_____________Имя_________Имя
hasMany()
, and to the flower model via hasManyThrough()
. Then we can calculate the total number of collections and colors of this manufacturer using withCount()
. belongsTo()
and communication with the color model via hasMany()
. We use withCount()
to count the total number of colors belonging to this collection. belongsTo()
, but we don't have feedback from the producers model. Laravel has no feedback for hasManyThrough()
.Answer the question
In order to leave comments, you need to log in
The question is so well asked that it makes me want to answer it!
Your structure is correct, as far as I understood from the description.
And sometimes such "link chains" are not from 3 links, but from 10 :)
It's just that Laravel has hasManyThrough through 1 table, but this does not oblige you to anything. I recently saw the hasManyThrough package for 2+ intermediate tables.
To get feedback on belongTo, just use this, no reverse hasManyThrough is needed here:
$color->collection;
$color->collection->manufacturer;
If I understand you correctly, a color has only one collection, and collections have only one producer. Then in the model you can simply make a method
return $this->collection()->first()->manufacturer();
The most important thing in database design is to thoroughly understand the subject area.
Strange condition. Can't two different collections be blue?
So an item can have multiple names?
Why not make a separate Collection table and a separate Color table and link them with an M:N table?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question