[英]Laravel Many-to-many relationship using pivot table
I'm creating an application wherein users can be assigned to multiple organisations, and organisations can have many users - many-to-many relationships. 我正在创建一个应用程序,其中可以将用户分配给多个组织,并且组织可以具有许多用户-多对多关系。
I want to be able to do something like this: $user->organisations()->get()
as well as $organisation->users()->get()
我希望能够做这样的事情: $user->organisations()->get()
以及$organisation->users()->get()
users
table... 我有一个users
表... organisations
table... 我有一个organisations
表... user_orgs
pivot/mapping/etc. 而且我有一个user_orgs
数据透视表/映射/等。 table 表 The user_orgs
table has an org_id
field, and a user_id
field. user_orgs
表具有一个org_id
字段和一个user_id
字段。
Eloquent assumes and expects organisation_user
- but that is not what I have named my table, nor do I want to rename. 雄辩的假设并期望organisation_user
但这不是我命名的表,也不是我想重命名的表。
I set up the following 我设置了以下内容
public function users(){
return $this->belongsToMany("App\User");
}
and on user: 并针对用户:
public function organisations(){
return $this->belongsToMany("App\Organisation");
}
And then I test this: 然后我测试一下:
Route::get('/test-route', function(){
$u = App\User::first();
return $u->organisations()->get();
});
and get the following error: 并得到以下错误:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.organisation_user' doesn't exist (SQL: select
organisations
.*,organisation_user
.user_id
aspivot_user_id
,organisation_user
.organisation_id
aspivot_organisation_id
fromorganisations
inner joinorganisation_user
onorganisations
.id
=organisation_user
.organisation_id
whereorganisation_user
.user_id
= 1) SQLSTATE [42S02]:基表或视图未找到:1146表'db.organisation_user'不存在(SQL:选择organisations
。*,organisation_user
。user_id
如pivot_user_id
,organisation_user
。organisation_id
作为pivot_organisation_id
从organisations
内部联接organisation_user
上organisations
。id
=organisation_user
。organisation_id
其中organisation_user
。user_id
= 1)
The two questions in my mind are, one; 我想到的两个问题是: how do I specify to Eloquent, to use user_orgs
instead of assuming organisation_user
, the same way I can override the tableName
or primaryKey
on a model? 我怎么指定侃侃而谈,使用user_orgs
而不是假设organisation_user
,以同样的方式我可以重写tableName
或primaryKey
一个模型? Two: how can I specify the foreignKey column, such that it uses user_orgs.org_id
and not user_orgs.organisation_id
? 第二:如何指定foreignKey列,使其使用user_orgs.org_id
而不使用user_orgs.organisation_id
?
But please do suggest alternatives. 但是请提出其他建议。
Thanks all, in advance. 谢谢大家,提前。
You can set the name of the pivot table by passing it as the second argument to the belongsToMany()
relationship, like below: 您可以通过将数据透视表的名称作为第二个参数传递给belongsToMany()
关系来设置数据透视表的名称,如下所示:
public function users(){
return $this->belongsToMany("App\User", "user_orgs");
}
public function organisations(){
return $this->belongsToMany("App\Organisation", "user_orgs");
}
Documentation: https://laravel.com/docs/5.8/eloquent-relationships#many-to-many 文档: https : //laravel.com/docs/5.8/eloquent-relationships#many-to-many
As mentioned previously, to determine the table name of the relationship's joining table, Eloquent will join the two related model names in alphabetical order. 如前所述,为了确定关系的联接表的表名,Eloquent将按字母顺序联接两个相关的模型名。 However, you are free to override this convention. 但是,您可以随意重写此约定。 You may do so by passing a second argument to the belongsToMany method. 您可以通过将第二个参数传递给belongsToMany方法来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.