我的代码如下:
return User::whereHas('roles', function ($role, $query) {
return $role;
$query->whereId($role);
})->get();
我正在尝试的是将角色 id 传递给查询生成器。
它最终出现以下错误:
Symfony\Component\Debug\Exception\FatalThrowableError
Too few arguments to function App\Http\Controllers\UserController::App\Http\Controllers\{closure}(), 1 passed in /Users/x/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 962 and exactly 2 expected
我想这就是你想要的:
闭包是一种在其自身环境中求值的函数,它具有一个或多个在调用函数时可以访问的绑定变量。
The use()
关键字让你从函数环境外部导入变量,在函数内部。
return User::whereHas('roles', function ($query) use ($role) {
return $query->whereId($role);
})->get();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)