【发布时间】:2020-08-24 04:05:45
【问题描述】:
我有一个多对多的关系,我用交集表解决了这个关系。 所以表 A 和 B 通过 AxB 连接。 A 和 AxB 是 1-n,B 和 AxB 是 1-n。
表的实际名称:
table A = extensiontables_registry
table B = ad_groups
table AxB = extensiontables_registryxad_groups
您可以在此处查看逻辑数据模型: https://imgur.com/MNpC3XV
我已经把我们现在正在谈论的部分放在一个红框里。
现在,我的后端 API 中有以下代码行:
$permittedTables = extensiontables_registry::findMany($ids)->pluck('extensiontable_name')->toArray();
为了简短起见,$ids 包含来自“ad_groups”的所有 ID。这些我是从按预期工作的获取中获得的。
根据我的日志,$ids 包含这些值/ID:
[1,2,3,4,5,6,7,8,9,10,12]
现在,extensiontables_registryxad_groups 目前看起来像这样:
select * from extensiontables_registryxad_groups;
+-----------------------------+-------------+------------+------------+
| extensiontables_registry_id | ad_group_id | created_at | updated_at |
+-----------------------------+-------------+------------+------------+
| 1 | 8 | NULL | NULL |
| 2 | 8 | NULL | NULL |
+-----------------------------+-------------+------------+------------+
2 rows in set (0.000 sec)
extensiontables_registry 看起来像这样:
+----+-----------------------+------------+------------+
| id | extensiontable_name | created_at | updated_at |
+----+-----------------------+------------+------------+
| 1 | extensiontable_itc | NULL | NULL |
| 2 | extensiontable_sysops | NULL | NULL |
| 3 | test | NULL | NULL |
+----+-----------------------+------------+------------+
现在的问题是我上面的代码sn-p:
$permittedTables = extensiontables_registry::findMany($ids)->pluck('extensiontable_name')->toArray();
返回这个结果:
array (
0 => 'extensiontable_itc',
1 => 'extensiontable_sysops',
2 => 'test',
)
所以coden-p 并没有做我想做的事。它应该只为我获取那些具有 ID 的扩展表的名称,这些扩展表的 ID 存在于 extensiontables_registryxad_groups 中的相同记录上,其 ID 来自我上面的 inputarray。所以我目前期望的结果是这样的:
array (
0 => 'extensiontable_itc',
1 => 'extensiontable_sysops'
)
我对 laravel 和 eloquent 很陌生,所以我真的不知道我在我的 coden-p 中做错了什么。我也不知道我能做些什么来让它按预期工作^^
为了完整起见,我将向您展示我针对这种表格排列的雄辩的模型/类,以防您可能需要它:
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 方法的信息,我将非常感激 ^^ 如果您需要更多信息,请正确提问离开:)
【问题讨论】:
标签: php laravel eloquent orm lumen