我有以下代码。
$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()的回调中不可见?我该怎么做才能 "把它弄进去"?
从父作用域中传递变量,使用 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();