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

Hel*_*815 3 php mysql laravel eloquent

我有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    |
+----+-----------+------------+-------------+---------+
Run Code Online (Sandbox Code Playgroud)

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

  • 用户(N) <-> (N)平台
  • 用户(N) <-> (N)国家
  • 用户(0..1) <-> (N)发布
  • 邮政(N) <-> (N)国家
  • 帖子(N) <-> (1)平台

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

  // Country.php
  public function posts()
  {
      return $this->belongsToMany('App\Post');
  }

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

  // Platform.php
  public function users()
  {
      return $this->belongsToMany('App\User');
  }

  public function posts()
  {
      return $this->belongsToMany('App\Post');
  }

  // User.php
  public function posts()
    {
        return $this->hasMany('App\Post');
    }

    public function countries()
    {
        return $this->hasMany('App\Country');
    }

    public function platforms()
    {
          return $this->hasMany('App\Platform');
    }

  // Post.php
  public function countries()
  {
      return $this->hasMany('App\Country');
  }

  public function platforms()
  {
      return $this->hasMany('App\Comment');
  }

  public function user()
  {
      return $this->belongsTo('App\User', 'user_id');
  }
Run Code Online (Sandbox Code Playgroud)

但是现在我很困惑,因为我认为在 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 |
+----+------------+---------+
Run Code Online (Sandbox Code Playgroud)

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

小智 5

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

首先请纠正您的关系定义。

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

public function countries(){
    return $this->belongsToMany(Country::class);
}
Run Code Online (Sandbox Code Playgroud)

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

public function users(){
    return $this->belongsToMany(User::class);
}
Run Code Online (Sandbox Code Playgroud)

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

php artisan make:migration create_country_user_table
Run Code Online (Sandbox Code Playgroud)

之后你需要完成你的表格:

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');
}
Run Code Online (Sandbox Code Playgroud)