P
P
Pr00f2015-02-13 13:57:43
Laravel
Pr00f, 2015-02-13 13:57:43

Eloquent - how to properly link models?

Can anyone help me link the models?
There is a main model - Gymnasium, a model with people - People and an intermediate model - GymnasiumMembers.
It is necessary to display a list of schools, which will include: the name of the school, the number of people from this school, the name of the school principal.
I can't imagine how to connect all this with the rest of the tables. There is a simplified sql query to make it clearer:

SELECT g.id, g.name, p.name AS director, gm.title,
  (SELECT COUNT(*) FROM gymnasium_members gm WHERE gm.gymnasium_id = g.id) AS members_count
FROM gymnasium as g
LEFT JOIN gymnasium_members AS gm ON gm.gymnasium_id = g.id
LEFT JOIN people AS p ON p.id = gm.people_id
WHERE gm.title = 'DIRECTOR'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gladkovskiy, 2015-02-13
@SMGladkovskiy

Can a person work in several gymnasiums at the same time? If not, then you don't need an intermediate table. The connection will be one (gymnasium) to many (person). And peoble will belongTo gymnasium and vice versa.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question