laravel 5.x中的hasMany vs EmiratesToMany [英] hasMany vs belongsToMany in laravel 5.x
问题描述
我很好奇为什么hasMany
的雄辩关系具有与belongsToMany
不同的签名.特别是自定义联接表名称-对于给定Comment
属于许多Role
且给定Role
会具有许多Comment
的系统,我想将关系存储在名为my_custom_join_table
,并将键设置为comment_key
和role_key
.
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
.
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');
如果我有一个Role
对象,该对象为hasMany
Comments
,但是我使用非标准表名称来存储该关系,为什么我可以使用这种非标准表来一种方式,而不能使用另一种方式呢?
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?
推荐答案
hasMany
用于多对多关系.它们都是不同的关系类型,每个都需要不同的数据库结构-因此它们采用不同的参数.
hasMany
is used in a One To Many relationship while belongsToMany
refers to a Many To Many relationship. They are both distinct relationship types and each require a different database structure - thus they take different parameters.
主要区别在于,在一对多关系中,您只需要两个与相关模型相对应的数据库表.这是因为对该关系的引用存储在拥有的模型的表本身中.例如,您可能有一个Country
模型和一个City
模型.一个国家有很多城市.但是,每个城市仅存在于一个国家/地区.因此,您可以将该国家存储在城市模型本身中(作为country_id
或类似名称).
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).
但是,多对多关系需要一个第三数据库表,称为数据透视表.数据透视表存储了对两个模型的引用,您可以在关系声明中将其声明为第二个参数.例如,假设您有City
模型,而您也有Car
模型.您需要一种关系来显示人们在每个城市驾驶的汽车的类型.好吧,在一个城市里,人们会驾驶许多不同类型的汽车.但是,如果您查看一种汽车,您也会知道它可以在许多个不同的城市中行驶.因此,不可能在任何一个模型上存储city_id
或car_id
,因为每个模型都有多个.因此,您将这些引用放在数据透视表中.
However, a Many To Many relationship requires a third database 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 many different types of car. However, if you look at one car type you will also know that it can be driven in many different 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.
根据经验,如果使用belongsToMany
关系,则只能 与另一个belongsToMany
关系配对,这意味着您具有第三个数据透视表.如果使用hasMany
关系,则它只能 与belongsTo
关系配对,并且不需要额外的数据库表.
As a rule of thumb, if you use a belongsToMany
relationship, it can only be paired with another belongsToMany
relationship and means that you have a third pivot table. If you use a hasMany
relationship, it can only be paired with a belongsTo
relationship and no extra database tables are required.
在您的示例中,您只需要将逆关系变成belongsToMany
,然后再次添加您的自定义表以及外键和本地键(与其他模型相反的顺序)即可.
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).
这篇关于laravel 5.x中的hasMany vs EmiratesToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!