【问题标题】:Why is eloquent adding underscore to the FK name?为什么 eloquent 在 FK 名称后面加下划线?
【发布时间】:2020-08-26 16:12:23
【问题描述】:

我之间有一个多对多的关系

extensiontables_registry

Ad_groups

数据透视表是extensiontables_registryxad_groups

现在我有了这个代码:

$permittedTables = extensiontables_registry::has("ad_groups")->get();

我想看看这对我有什么好处,所以我这样做了:

log::info($permittedTables);

我得到这个错误:

[2020-05-11 07:32:23] local.ERROR: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'extensiontables_registryxad_groups.extensiontables__registry_id' in 'where clause' in E:\aether-backend\vendor\illuminate\database\Connection.php:331

据我所见,laravel/eloquent 会自动在 extensiontables_registryxad_groups 数据透视表上的 extensiontables_registry_id 列中添加下划线。为什么这样做?如何预防?

三个表目前是这样的:

extensiontables_registry:

+----+-----------------------+------------+------------+
| id | extensiontable_name   | created_at | updated_at |
+----+-----------------------+------------+------------+
|  1 | extensiontable_itc    | NULL       | NULL       |
|  2 | extensiontable_sysops | NULL       | NULL       |
|  4 | test                  | NULL       | NULL       |
+----+-----------------------+------------+------------+

extensiontables_registryxad_groups:

+-----------------------------+-------------+------------+------------+
| extensiontables_registry_id | ad_group_id | created_at | updated_at |
+-----------------------------+-------------+------------+------------+
|                           1 |           8 | NULL       | NULL       |
|                           2 |           8 | NULL       | NULL       |
+-----------------------------+-------------+------------+------------+

广告组:

+----+------------------------------------+---------------------+---------------------+
| id | name                               | created_at          | updated_at          |
+----+------------------------------------+---------------------+---------------------+
|  1 | test16                             | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
|  2 | test15                             | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
|  3 | test14                             | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
|  4 | test13                             | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
|  5 | test12                             | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
|  6 | test11                             | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
|  7 | test10                             | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
|  8 | test9                              | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
|  9 | test8                              | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
| 10 | test7                              | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
| 11 | test6                              | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
| 12 | test5                              | 2020-02-26 09:30:21 | 2020-02-26 09:30:21 |
| 13 | test4                              | 2020-03-16 13:27:24 | 2020-03-16 13:27:24 |
| 14 | test3                              | 2020-03-16 13:27:24 | 2020-03-16 13:27:24 |
| 15 | test2                              | 2020-03-16 13:27:24 | 2020-03-16 13:27:24 |
| 16 | test                               | 2020-03-16 13:27:24 | 2020-03-16 13:27:24 |
+----+------------------------------------+---------------------+---------------------+

所以这个查询肯定应该有一些结果,对吧?此外,查询本身运行,但是当我尝试记录它时,我得到了上述错误。

为了完整起见,以下是为 eloquent 定义模型的类:

AD_Group.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Ad_group extends Model
{
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [
    'name'
  ];

  /**
   * Hides pivot from return queries.
   *
   * @var array
   */
  protected $hidden = [
    'pivot'
  ];

  /**
   * Many-To-Many relationship with User-Model.
   */
  public function Ad_users()
  {
    return $this->belongsToMany('App\Ad_user', 'Ad_usersxad_groups', 'Ad_group_id', 'Ad_user_id');
  }

  public function extensiontables()
  {
    return $this->belongsToMany('App\extensiontables_registry', 'extensiontables_registryxad_groups');
  }

}

还有 Extensiontables_registry.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Extensiontables_Registry extends Model
{

  /**
 * The table associated with the model.
 *
 * @var string
 */
 protected $table = 'Extensiontables_Registry';

  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [
    'extensiontable_name'
  ];



  /**
   * Many-To-Many relationship with User-Model.
   */
  public function Ad_groups()
  {
    return $this->belongsToMany('App\Ad_group', 'extensiontables_registryxad_groups');
  }
}

【问题讨论】:

    标签: laravel eloquent orm


    【解决方案1】:

    好的,我自己找到了答案: https://laracasts.com/discuss/channels/laravel/custom-name-and-foreign-key-for-manytomany-relationship

    TL/DR: Laravel/Eloquent 在数据透视表上“猜测”我的 FK 属性的名称时遇到问题。所以我必须明确定义它们。在此处查看 laravel 文档: https://laravel.com/docs/master/eloquent-relationships#many-to-many

    "除了自定义加入表的名称外,还可以通过给belongsToMany方法传递额外的参数来自定义表上键的列名。第三个参数是模型的外键名。您正在定义关系,而第四个参数是您要加入的模型的外键名称:

    return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
    

    "

    就我而言,在 extensiontables_registry.php 我需要这个:

      public function Ad_groups()
      {
        return $this->belongsToMany('App\Ad_group', 'extensiontables_registryxad_groups', 'extensiontables_registry_id', 'ad_group_id');
      }
    

    在 AD_Group.php 上我需要这个:

      public function extensiontables()
      {
        return $this->belongsToMany('App\extensiontables_registry', 'extensiontables_registryxad_groups', 'ad_group_id', 'extensiontables_registry_id');
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-05
      • 2014-09-28
      • 2018-03-01
      • 2012-10-23
      相关资源
      最近更新 更多