php laravel 5.x 中的 hasMany 与belongsToMany
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36208460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hasMany vs belongsToMany in laravel 5.x
提问by user101289
I'm curious why the Eloquent relationship for hasMany
has a different signature than for belongsToMany
. Specifically the custom join table name-- for a system where a given Comment
belongs to many Role
s, and a given Role
would have many Comment
s, I want to store the relationship in a table called my_custom_join_table
and have the keys set up as comment_key
and role_key
.
我很好奇为什么 Eloquent 关系 forhasMany
的签名与 for 不同belongsToMany
。特别是自定义连接表名称 - 对于一个给定Comment
属于许多Role
s 的系统,并且一个给定Role
将有许多Comment
s,我想将关系存储在一个名为的表中my_custom_join_table
,并将键设置为comment_key
and role_key
。
return $this->belongsToMany('App\Role', 'my_custom_join_table', 'comment_key', 'role_key'); // works
But on the inverse, I can't define that custom table (at least the docs don't mention it):
但反过来,我无法定义该自定义表(至少文档没有提及):
return $this->hasMany('App\Comment', 'comment_key', 'role_key');
If I have a Role
object that hasMany
Comments
, but I use a non-standard table name to store that relationship, why can I use this non-standard table going one way but not the other?
如果我有一个Role
对象hasMany
Comments
,但我使用非标准表名来存储该关系,为什么我可以以一种方式使用这个非标准表而不是另一种方式?
回答by Andy Noelker
hasMany
is used in a One To Manyrelationship while belongsToMany
refers to a Many To Manyrelationship. They are both distinct relationship types and each require a different database structure - thus they take different parameters.
hasMany
用于一对多关系,而belongsToMany
指代多对多关系。它们都是不同的关系类型,每种都需要不同的数据库结构 - 因此它们采用不同的参数。
The key difference is that in a One To Many relationship, you only need the two database tables that correspond to the related models. This is because the reference to the relation is stored on the owned model's table itself. For instance, you might have a Country
model and a City
model. A Country has many cities. However, each City only exists in one country. Therefore, you would store that country on the City model itself(as country_id
or something like that).
关键区别在于,在一对多关系中,您只需要与相关模型对应的两个数据库表。这是因为对关系的引用存储在拥有模型的表本身中。例如,您可能有一个Country
模型和一个City
模型。一个国家有许多城市。但是,每个城市仅存在于一个国家/地区。因此,您可以将该国家/地区存储在 City 模型本身上(country_id
或类似的东西)。
However, a Many To Many relationship requires a thirddatabase table, called a pivot table. The pivot table stores references to both the models and you can declare it as a second parameter in the relationship declaration. For example, imagine you have your City
model and you also have a Car
model. You want a relationship to show the types of cars people drive in each city. Well, in one city people will drive manydifferent types of car. However, if you look at one car type you will also know that it can be driven in manydifferent cities. Therefore it would be impossible to store a city_id
or a car_id
on either model because each would have more than one. Therefore, you put those references in the pivot table.
但是,多对多关系需要第三个数据库表,称为数据透视表。数据透视表存储对两个模型的引用,您可以将其声明为关系声明中的第二个参数。例如,假设你有你的City
模型,你也有一个Car
模型。你想要一个关系来显示人们在每个城市驾驶的汽车类型。嗯,在一个城市里,人们会驾驶许多不同类型的汽车。但是,如果您查看一种汽车类型,您也会知道它可以在许多不同的城市行驶。因此,不可能存储 acity_id
或 acar_id
在任一模型上,因为每个模型都有多个。因此,您将这些引用放在数据透视表中。
As a rule of thumb, if you use a belongsToMany
relationship, it can onlybe paired with another belongsToMany
relationship and means that you have a third pivot table. If you use a hasMany
relationship, it can onlybe paired with a belongsTo
relationship and no extra database tables are required.
根据经验,如果您使用一个belongsToMany
关系,它只能与另一个belongsToMany
关系配对,这意味着您有第三个数据透视表。如果使用hasMany
关系,则只能与belongsTo
关系配对,不需要额外的数据库表。
In your example, you just need to make the inverse relation into a belongsToMany
and add your custom table again, along with the foreign and local keys (reversing the order from the other model).
在您的示例中,您只需要将逆关系变为 abelongsToMany
并再次添加您的自定义表,以及外键和本地键(反转其他模型的顺序)。
回答by Kabir Hossain
Try to understand with text and a figure.
试着用文字和图来理解。
One to One(hasOne) relationship:
- A user has(can have) one profile. So, a profile belongs to one user.
One to many(hasMany):
- A user has many(can have many) articles. So, many articles belong to one user.
Many to many(BelongsToMany):
- A User can belong to many forums. So, a forum belongs to many users.