为什么whereHas()的回调中看不到$ids?

问题描述 投票:0回答:1

我有以下代码。

$ids = $adGroupsOfUser->pluck('id');
$permittedTables = extensiontables_registry::whereHas('ad_groups', function($q){
  $q->whereIn("ad_group_id", $ids);
})->pluck('extensiontable_name')->toArray();

我得到这个错误:

local.ERROR: ErrorException: Undefined variable: ids in E:\aether-backend\app\Http\Helpers\modelInteractions.php:51

为什么 $ids 在whereHas()的回调中不可见?我该怎么做才能 "把它弄进去"?

php laravel eloquent orm lumen
1个回答
1
投票

从父作用域中传递变量,使用 use(...vars)

$ids = $adGroupsOfUser->pluck('id');
$permittedTables = extensiontables_registry::whereHas('ad_groups', function($q) use($ids) {
  $q->whereIn("ad_group_id", $ids);
})->pluck('extensiontable_name')->toArray();
© www.soinside.com 2019 - 2024. All rights reserved.