問題描述
我很好奇為什么 hasMany
的 Eloquent 關(guān)系與 belongsToMany
的簽名不同.特別是自定義連接表名稱——對于給定 Comment
屬于許多 Role
的系統(tǒng),并且給定的 Role
將有許多 Comment
s,我想將關(guān)系存儲在一個名為 my_custom_join_table
的表中,并將鍵設(shè)置為 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('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
Comments
的 Role
對象,但我使用非標(biāo)準(zhǔn)表名來存儲該關(guān)系,為什么可以我以一種方式使用這個非標(biāo)準(zhǔn)表而不是另一種方式?
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
用于 一對多關(guān)系,而 belongsToMany
指的是 多對多關(guān)系.它們都是不同的關(guān)系類型,每種都需要不同的數(shù)據(jù)庫結(jié)構(gòu) - 因此它們采用不同的參數(shù).
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.
關(guān)鍵區(qū)別在于,在一對多關(guān)系中,您只需要與相關(guān)模型對應(yīng)的兩個數(shù)據(jù)庫表.這是因為對關(guān)系的引用存儲在擁有模型的表本身中.例如,您可能有一個 Country
模型和一個 City
模型.一個國家有許多城市.但是,每個城市僅存在于一個國家/地區(qū).因此,您可以將該國家/地區(qū)存儲在 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).
但是,多對多關(guān)系需要第三個??數(shù)據(jù)庫表,稱為數(shù)據(jù)透視表.數(shù)據(jù)透視表存儲對兩個模型的引用,您可以將其聲明為關(guān)系聲明中的第二個參數(shù).例如,假設(shè)您有 City
模型和 Car
模型.你想要一個關(guān)系來顯示人們在每個城市駕駛的汽車類型.好吧,在一個城市里,人們會駕駛許多 種不同類型的汽車.但是,如果您查看一種汽車類型,您也會知道它可以在許多不同的城市中行駛.因此,不可能在任一模型上存儲 city_id
或 car_id
,因為每個模型都有多個.因此,您將這些引用放入數(shù)據(jù)透視表中.
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.
根據(jù)經(jīng)驗,如果您使用 belongsToMany
關(guān)系,它只能與另一個 belongsToMany
關(guān)系配對,這意味著您有第三個數(shù)據(jù)透視表.如果您使用 hasMany
關(guān)系,它可以只與 belongsTo
關(guān)系配對,并且不需要額外的數(shù)據(jù)庫表.
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.
在您的示例中,您只需要將逆關(guān)系轉(zhuǎn)換為 belongsToMany
并再次添加您的自定義表,以及外鍵和本地鍵(反轉(zhuǎn)其他模型的順序).
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).
這篇關(guān)于laravel 5.x 中的 hasMany 與 BeingToMany的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!