Reputation: 3573
I have a one-to-many relationship for Gym
to User
relationship. That being a Gym
can have multiple users, but a User
can only have one gym.
Is it better to have a pivot table for this to link gym_id
to user_id
like follows?
Gym_User
Table:
gym_id | user_id
or in the User
table like follows?
User
Table:
id | name | password | gym_id
Upvotes: 1
Views: 2380
Reputation: 2117
You have a 1 to many relationships, in this case you donot need pivot table, gym_id is foreign key in the user table and you will define relations on the models.
In gym model you will define a hasMany relationship to user model and in user model define belongsto relationship to gym model. (Refer to Laravel documentation).
One of nice example for pivot table is Access Controller Models Such as User,Role and Permission..
Upvotes: 2
Reputation: 1103
Pivot tables are used when having many-to-many relationship.But for your case, you have One-to-many relationship. And don't need a pivot table.
A typical example of many-to-many relationship is Article & Tags. Where 1 article can have many tags and 1 tag can belong to many articles.
But yours is simple one-to-many relationship.
Upvotes: 7