简体   繁体   English

Laravel使用数据透视表的多对多关系

[英]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()

  • I have a users table... 我有一个users表...
  • I have an organisations table... 我有一个organisations表...
  • Uh! 呃! users-organisations, organisations-users! 用户组织,组织用户!
  • and I have a user_orgs pivot/mapping/etc. 而且我有一个user_orgs数据透视表/映射/等。 table

Note 注意

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 我设置了以下内容

App/Organisation.php 应用/ Organisation.php

public function users(){
    return $this->belongsToMany("App\User");
}

and on user: 并针对用户:

App/User.php 应用/ user.php的

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 as pivot_user_id , organisation_user . organisation_id as pivot_organisation_id from organisations inner join organisation_user on organisations . id = organisation_user . organisation_id where organisation_user . user_id = 1) SQLSTATE [42S02]:基表或视图未找到:1146表'db.organisation_user'不存在(SQL:选择organisations 。*, organisation_useruser_idpivot_user_idorganisation_userorganisation_id作为pivot_organisation_idorganisations内部联接organisation_userorganisationsid = organisation_userorganisation_id其中organisation_useruser_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 ,以同样的方式我可以重写tableNameprimaryKey一个模型? 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM