简体   繁体   English

Laravel/Eloquent ORM - 仅检索引用的记录

[英]Laravel/Eloquent ORM - retrieve only referenced records

I have a many-to-many relationship which I resolved with an intersection table.我有一个多对多的关系,我用一个交集表解决了这个关系。 So table A and B are connected through AxB.所以表 A 和 B 通过 AxB 连接。 A and AxB are 1-n and B and AxB are 1-n. A 和 AxB 是 1-n,B 和 AxB 是 1-n。

The actual names of the tables:表的实际名称:

   table A = extensiontables_registry
   table B = ad_groups
   table AxB = extensiontables_registryxad_groups

You can see the logical datamodel here: https://imgur.com/MNpC3XV您可以在此处查看逻辑数据模型: https://imgur.com/MNpC3XV

Ive put the part we are talking about right now into a red frame.我已经把我们现在谈论的部分放在了一个红框里。

Now, I have the following line of code in my backend-API:现在,我的后端 API 中有以下代码行:

$permittedTables = extensiontables_registry::findMany($ids)->pluck('extensiontable_name')->toArray();

To keep things short, the $ids contains all the ids from "ad_groups".为了简短起见, $ids包含来自“ad_groups”的所有 id。 These I've gotten from a fetch which works as intended.这些我是从按预期工作的获取中获得的。 The $ids contains these values/ids according to my logs:根据我的日志, $ids包含这些值/id:

[1,2,3,4,5,6,7,8,9,10,12]  

Now, the extensiontables_registryxad_groups currently looks like this:现在,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)

And the extensiontables_registry looks like this: extensiontables_registry 看起来像这样:

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

And now the problem is that my codesnippet from above:现在的问题是我的代码片段来自上面:

$permittedTables = extensiontables_registry::findMany($ids)->pluck('extensiontable_name')->toArray();

returns me this result:给我这个结果:

 array (
  0 => 'extensiontable_itc',
  1 => 'extensiontable_sysops',
  2 => 'test',
)  

So the codesnippet does NOT do what I want it to do.所以代码片段没有做我想做的事。 It should only fetch me the names of those extensiontables which have IDs which exist on the very same record(s) in extensiontables_registryxad_groups with IDs from my inputarray above.它应该只为我获取那些具有 ID 的扩展表的名称,这些扩展表的 ID 存在于 extensiontables_registryxad_groups 中的相同记录上,其 ID 来自我上面的 inputarray。 So The result I currently would expect would be this:所以我目前期望的结果是:

 array (
  0 => 'extensiontable_itc',
  1 => 'extensiontable_sysops'
)  

I am pretty new to laravel and eloquent, so I dont really know what I did wrong in my codesnippet.我对 laravel 和 eloquent 很陌生,所以我真的不知道我在代码片段中做错了什么。 I also have no idea what I can do to get this working as intended ^^我也不知道我能做些什么来让它按预期工作^^

For the sake of completeness, I'll show you my eloquent models/classes for this arrangement of tables, just in case you might need it:为了完整起见,我将向您展示我的 eloquent 模型/类,以防万一您可能需要它:

AD_Group.php: 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: 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');
  }
}

I would be really thankful if you could help me or at least give me a hint what to look out for in order to find information on leveraging the methods of laravel/eloquent accordingly ^^ If you need any more info, ask right away:)如果您能帮助我或至少给我一个提示,以便找到有关利用 laravel/eloquent 方法的信息,我将非常感激 ^^ 如果您需要更多信息,请立即询问:)

I think you got things a little mixed up.我觉得你把事情搞混了。

what this query does:此查询的作用:

$permittedTables = extensiontables_registry::findMany($ids)->pluck('extensiontable_name')->toArray();

is access the extensiontables_registry table and get records with the same array of $ids you sent.是访问extensiontables_registry表并获取与您发送的相同$ids数组的记录。

But it will NOT take in consideration the relationship with extensiontables_registryxad_groups table because you didn't explicitly specify it in the query.但它不会考虑与extensiontables_registryxad_groups表的关系,因为您没有在查询中明确指定它。

What you should add is has('relationship name') which get the records of this model that only has a corresponding Foreign key in relationship name' in your case Ad_groups您应该添加的是has('relationship name') ,它会在您的情况下获取此 model 的记录,该记录在relationship name'中只有一个相应的外键' Ad_groups

So in this case the query would look like this:所以在这种情况下,查询将如下所示:

$permittedTables = extensiontables_registry::has('Ad_groups')->pluck('extensiontable_name')->toArray();

i think you can get there using join:我认为您可以使用 join 到达那里:

$permittedTables = extensiontables_registry::whereIn('id', $ids)
->join('extensiontables_registryxad_groups','extensiontables_registryxad_groups'.'extensiontables_registry_id','extensiontables_registry.id')
  ->select('extensiontables_registry.extensiontable_name')->get()->all();

please note you can avoid repeated results using 'distinct';请注意,您可以使用 'distinct' 避免重复的结果;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM