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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:44:58  来源:igfitidea点击:

hasMany vs belongsToMany in laravel 5.x

phplaraveleloquent

提问by user101289

I'm curious why the Eloquent relationship for hasManyhas a different signature than for belongsToMany. Specifically the custom join table name-- for a system where a given Commentbelongs to many Roles, and a given Rolewould have many Comments, I want to store the relationship in a table called my_custom_join_tableand have the keys set up as comment_keyand role_key.

我很好奇为什么 Eloquent 关系 forhasMany的签名与 for 不同belongsToMany。特别是自定义连接表名称 - 对于一个给定Comment属于许多Roles 的系统,并且一个给定Role将有许多Comments,我想将关系存储在一个名为的表中my_custom_join_table,并将键设置为comment_keyand 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 Roleobject that hasManyComments, 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对象hasManyComments,但我使用非标准表名来存储该关系,为什么我可以以一种方式使用这个非标准表而不是另一种方式?

回答by Andy Noelker

hasManyis used in a One To Manyrelationship while belongsToManyrefers 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 Countrymodel and a Citymodel. 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_idor 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 Citymodel and you also have a Carmodel. 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_idor a car_idon 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 belongsToManyrelationship, it can onlybe paired with another belongsToManyrelationship and means that you have a third pivot table. If you use a hasManyrelationship, it can onlybe paired with a belongsTorelationship and no extra database tables are required.

根据经验,如果您使用一个belongsToMany关系,它只能与另一个belongsToMany关系配对,这意味着您有第三个数据透视表。如果使用hasMany关系,则只能belongsTo关系配对,不需要额外的数据库表。

In your example, you just need to make the inverse relation into a belongsToManyand 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.

试着用文字和图来理解。

  1. One to One(hasOne) relationship:

    • A user has(can have) one profile. So, a profile belongs to one user.
  2. One to many(hasMany):

    • A user has many(can have many) articles. So, many articles belong to one user.
  3. Many to many(BelongsToMany):

    • A User can belong to many forums. So, a forum belongs to many users.

    RelationShip

  1. 一对一(hasOne)关系:

    • 一个用户有(可以有)一个配置文件。因此,一个配置文件属于一个用户。
  2. 一对多(hasMany):

    • 一个用户有很多(可以有很多)文章。因此,许多文章属于一个用户。
  3. 多对多(BelongsToMany):

    • 一个用户可以属于多个论坛。因此,一个论坛属于许多用户。

    关系