Laravel:使用Eloquent ORM的N到N(mysql)关系



我有4张桌子:

// Table countries
+----+------+
| Id | Name |
+----+------+
|  1 | USA  |
|  2 | GB   |
+----+------+
// Table platforms
+----+---------+
| Id |  Name   |
+----+---------+
|  1 | Windows |
|  2 | Linux   |
+----+---------+
// Table users
+----+-------+------------+-------------+
| Id | Name  | country_id | platform_id |
+----+-------+------------+-------------+
|  1 | Admin |          1 |           1 |
|  2 | Test  |          2 |           1 |
+----+-------+------------+-------------+
// Table posts
+----+-----------+------------+-------------+---------+
| Id |   Title   | country_id | platform_id | user_id |
+----+-----------+------------+-------------+---------+
|  1 | TestPost1 |          2 |           1 | 1       |
|  2 | TestPost2 |          2 |           2 | null    |
+----+-----------+------------+-------------+---------+

数据库应该能够实现以下关系:

  • 用户 (N( <-> (N( 平台
  • 用户 (N( <-> (N( 国家/地区
  • 用户 (0..1( <-> (N( 开机自检
  • 邮政 (N( <-> (N( 国家
  • 邮政 (N( <-> (1( 平台

所以现在我尝试按照 Laravel Eloquent ORM 文档实现这些关系:

  // Country.php
  public function posts()
  {
      return $this->belongsToMany('AppPost');
  }
  public function users()
  {
      return $this->belongsToMany('AppUser');
  }
  // Platform.php
  public function users()
  {
      return $this->belongsToMany('AppUser');
  }
  public function posts()
  {
      return $this->belongsToMany('AppPost');
  }
  // User.php
  public function posts()
    {
        return $this->hasMany('AppPost');
    }
    public function countries()
    {
        return $this->hasMany('AppCountry');
    }
    public function platforms()
    {
          return $this->hasMany('AppPlatform');
    }
  // Post.php
  public function countries()
  {
      return $this->hasMany('AppCountry');
  }
  public function platforms()
  {
      return $this->hasMany('AppComment');
  }
  public function user()
  {
      return $this->belongsTo('AppUser', 'user_id');
  }

但是现在我很困惑,因为我认为在mysql中实现N到N关系的方法是向db添加第三个表,例如:

// Table CountryUserRelations to implement User (N) <-> (N) Country
+----+------------+---------+
| Id | country_id | user_id |
+----+------------+---------+
|  1 |          1 |       1 |
|  2 |          2 |       1 |
|  3 |          1 |       2 |
|  4 |          2 |       2 |
+----+------------+---------+

但是Eloquent ORM如何处理我的模型中的规则呢?它会保留 N 到 N 的关系而不必添加关系表吗?还是我错过了什么或误解了雄辩的ORM关系概念?

我刚刚加入stackoverflow,所以我没有足够的信用来评论,所以我会在这里留下一个asnwer。

首先,请更正您的关系定义。

在用户模型中:(你在这里有错误(

public function countries(){
    return $this->belongsToMany(Country::class);
}

在您的国家/地区模型中:

public function users(){
    return $this->belongsToMany(User::class);
}

其次,您需要使用以下方法创建country_user表:

php artisan make:migration create_country_user_table

之后,您需要完成您的表格:

Schema::create('country_user', function (Blueprint $table){
    $table->increments('id');
    $table->unsignedInteger('country_id');
    $table->unsignedInteger('user_id');
    $table->foreign('country_id')->references('id')->on('countries');
    $table->foreign('user_id')->references('id')->on('users');
}

最新更新