为什么雄辩地在FK名称上添加下划线?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么雄辩地在FK名称上添加下划线?相关的知识,希望对你有一定的参考价值。
我之间有多对多关系
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-backendvendorilluminatedatabaseConnection.php:331
据我所知,laravel / eloquent自动为我的extensiontables_registry_id
数据透视表的extensiontables_registryxad_groups
列添加下划线。为什么这样做呢?如何预防?
当前三个表如下:
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 |
+----+------------------------------------+---------------------+---------------------+
因此,此查询肯定会有一些结果,对吗?此外,查询本身也会运行,但是当我尝试对其进行记录时,会出现上述错误。
为了完整起见,以下是为雄辩而定义模型的类:
AD_Group.php:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
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('AppAd_user', 'Ad_usersxad_groups', 'Ad_group_id', 'Ad_user_id');
}
public function extensiontables()
{
return $this->belongsToMany('Appextensiontables_registry', 'extensiontables_registryxad_groups');
}
}
和Extensiontables_registry.php:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
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('AppAd_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('AppRole', 'role_user', 'user_id', 'role_id');
“
就我而言,在extensiontables_registry.php上,我需要这个:
public function Ad_groups()
{
return $this->belongsToMany('AppAd_group', 'extensiontables_registryxad_groups', 'extensiontables_registry_id', 'ad_group_id');
}
并且在AD_Group.php上,我需要这个:
public function extensiontables()
{
return $this->belongsToMany('Appextensiontables_registry', 'extensiontables_registryxad_groups', 'ad_group_id', 'extensiontables_registry_id');
}
以上是关于为什么雄辩地在FK名称上添加下划线?的主要内容,如果未能解决你的问题,请参考以下文章