【发布时间】: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');
}
}
【问题讨论】: