laravel - 为什么 eloquent 在 FK 名字后面加下划线?

标签 laravel eloquent orm

我之间存在多对多关系

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 列添加下划线。为什么要这样做?我该如何预防?

这三个表目前如下所示:

扩展表注册表:

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

最佳答案

好吧,我自己找到了答案: 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');
  }

关于laravel - 为什么 eloquent 在 FK 名字后面加下划线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61724301/

相关文章:

Laravel 5.1 Snappy pdf 图像未在 pdf 文件中呈现

ajax - 通过 Ajax Laravel DataTable 返回 html 内容(使用 yajrabox 包)

php - 在 Heroku 中部署 Laravel 项目后 MySQL 不起作用

php - Laravel Eloquent 模型 id 作为字符串返回错误值

php - 我是否正确地进行了预加载? ( Eloquent )

javascript - 从 php/laravel 循环遍历集合,并将每次迭代中的一个项目添加到 jsPDF 捆绑文档

php - 拉维尔 5.4 : How to order by count(column) in eloquent?

Laravel:columnize() 必须是数组类型,给定字符串,调用

c# - 使用 Dapper 点网映射域实体的私有(private)属性

sql - 默认情况下根据原始查询将行添加到 Eloquent Collection