Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mithridates's avatar

How I define this relationship

Hi,
I have 3 tables:
venues:

id  |   name

locations:


id  |   address |   venue_id        |   city_id 

cities:

id  |   name    |   slug        |   display_name

a venue hasOne location, a location belongsTo a venue.
a location belongsTo a city,a city hasMany locations.
now, what's the relation between a venue and a city ?
how can I get queries from venue based on filtering city I mean execute "where" on city slug.
How can I get all venues from a city model?

0 likes
6 replies
belisar's avatar

Yeah, maybe I misunderstood. It escapes me but just out of curiosity, why is the venue thing in a separate table and not in the locations table as a field?

Mithridates's avatar

think of venue as a store in a street. it has a location. and a location belongsTo a city

belisar's avatar

Yes, but based on the cardinality of their relationship in this case it seems that for all intents and purposes the venue is not but an "attribute" of the location, which can eventually be null (there might not be a store in that location, but you still want the location in). Consider a middle name field. A middle name belongs to a user, a user has one middle name. However there is no need to make a separate middle name table and define a one to one cardinality with the user table. A middle name can also be null.

Having it in a separate table would cause unnecessary overhead for your database queries. You would only want to do it if you have fields which you rarely query against and as such it would improve performance to have them in a separate table. Since as I understand you plan on actually regularly querying the venues, having them on the same table with locations might benefit you more.

Mithridates's avatar

Found the relationship, it's called BelongsToThrough which Laravel hasn't yet!
(at last found sth laravel lacks!)
link

Please or to participate in this conversation.