简体   繁体   English

如何将数组传递给 ::with() - Lumen/Laravel

[英]How to pass an array to ::with() - Lumen/Laravel

So $permittedTables is an array of tablenames which I want to join to coretable .所以$permittedTables是我想加入到coretable的表名数组。 To do this, I want to use Model::with(), like so:为此,我想使用 Model::with(),如下所示:

$join = coretable::with($permittedTables)->get();

However, I get this error when executing the above code:但是,执行上述代码时出现此错误:

Argument 1 passed to Illuminate\Database\Eloquent\Builder::parseWithRelations() must be of the type array, object given, called in E:\aether-backend\vendor\illuminate\database\Eloquent\Builder.php on line 1043

Whats confusing me the most is the fact this actual IS an array, so I dont really understand why it actually throws THIS error xD最让我困惑的是这个实际是一个数组,所以我真的不明白为什么它实际上会抛出这个错误 xD

Still, can anyone tell me if this approach is actually possible?不过,谁能告诉我这种方法是否真的可行? And if so, how can I do it?如果是这样,我该怎么做?

EDIT: This is how the array was generated:编辑:这是数组的生成方式:

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

Just in case this has anything to do with it.以防万一这与它有关。

The pluck function return Collection/Enumerable, you have to call toArray function to get array of table names pluck函数返回Collection/Enumerable,必须调用toArray函数来获取表名数组

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

$permittedTables = extensiontables_registry::findmany($ids)->pluck('extensiontable_name'); In above code fragment ->pluck() method returns Object, that's why it throws following error must be of the type array, object given在上面的代码片段->pluck()方法返回对象,这就是为什么它抛出以下错误must be of the type array, object given

Try using this: $permittedTables = extensiontables_registry::findmany($ids)->pluck('extensiontable_name')->toArray() It should work.尝试使用这个: $permittedTables = extensiontables_registry::findmany($ids)->pluck('extensiontable_name')->toArray()它应该可以工作。

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

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