沃梦达 / 编程问答 / php问题 / 正文

laravel 5.x 中的 hasMany 与 BeingToMany

hasMany vs belongsToMany in laravel 5.x(laravel 5.x 中的 hasMany 与 BeingToMany)

本文介绍了laravel 5.x 中的 hasMany 与 BeingToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇为什么 hasMany 的 Eloquent 关系与 belongsToMany 的签名不同.特别是自定义连接表名称——对于给定 Comment 属于许多 Role 的系统,并且给定的 Role 将有许多 Comments,我想将关系存储在一个名为 my_custom_join_table 的表中,并将键设置为 comment_keyrole_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 Roles, and a given Role would have many Comments, 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('AppRole', '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('AppComment', 'comment_key', 'role_key');

如果我有一个 hasMany CommentsRole 对象,但我使用非标准表名来存储该关系,为什么可以我以一种方式使用这个非标准表而不是另一种方式?

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 用于 一对多关系,而 belongsToMany 指的是 多对多关系.它们都是不同的关系类型,每种都需要不同的数据库结构 - 因此它们采用不同的参数.

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 模型.一个国家有许多城市.但是,每个城市仅存在于一个国家/地区.因此,您可以将该国家/地区存储在 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_idcar_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 与 BeingToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:laravel 5.x 中的 hasMany 与 BeingToMany

基础教程推荐