【问题标题】:Laravel: N to N (mysql) relation using Eloquent ORMLaravel:使用 Eloquent ORM 的 N 到 N(mysql)关系
【发布时间】:2023-03-14 04:45:01
【问题描述】:

我有 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('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');
  }

但现在我很困惑,因为我认为在mysql中实现N to 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 的关系而不必添加关系表吗?还是我遗漏了什么或误解了 Eloquent ORM 关系概念?

【问题讨论】:

  • 表应该命名为country_user。不需要文件。 Laravel 查找由按字母顺序排列的下划线分隔的单数名称。
  • 所以我需要手动添加这个 country_user 表(而不是 CountryUserRelations )或使用 php artisan make:migration create_country_user_table?现在我用php artisan make:model User 创建了模型,用php artisan make:migration create_users_table 创建了表shemas。如果我使用 Eloquent ORM 语法,关系是否会自动添加到此表中?
  • 是的,您将使用 migrate 方法制作表格。不,不会自动创建关系。你仍然需要做$table-&gt;integer('user_id')-&gt;unsigned()-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade')-&gt;onUpdate('cascade')

标签: php mysql laravel eloquent


【解决方案1】:

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

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

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

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');
}

【讨论】:

  • 我为什么不定义Many ThroughPolymorphic 关系?文档说“多态关系允许一个模型在一个关联上属于多个其他模型。”这是平台表的情况吗?由于帖子最多只有一个平台,但用户喜欢多少就多少。但两者都使用相同的数据。或者我需要platform_userplatform_postcountry_usercountry_post
  • Many Trough 是另一回事。假设一个user 有很多posts,而一个Post 有很多comments,那么user has many comments trough post 。对于多态关系也有不同的故事。假设在您的应用程序中,您有一个名为Like 的模型,用户可以喜欢commentpost。所以Like 属于CommentPost,但不能同时属于两者。这里我们使用多态来定义单个表中的关系。请让我知道任何可能的问题。
  • 好的,谢谢。我设法使用存储关系的附加表以及模型内的相应定义正确设置它。
猜你喜欢
  • 2020-03-25
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
  • 1970-01-01
  • 2015-04-16
相关资源
最近更新 更多