将条件参数传递给幼虫的急切加载错误结束 [英] Passing conditional param to Eager Loading in larave ends with error
本文介绍了将条件参数传递给幼虫的急切加载错误结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下:
return User::whereHas('roles', function ($role, $query) {
return $role;
$query->whereId($role);
})->get();
我正在尝试将此处的角色ID传递给查询生成器.
what I am trying is to pass role id here to query builder.
它最终出现以下错误:
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
推荐答案
我认为这是您想要的:
闭包是在其自己的环境中评估的函数,该函数具有一个或多个绑定变量,调用该函数时可以访问它们.
A closure is a function that is evaluated in its own environment, which has one or more bound variables that can be accessed when the function is called.
通过use()
关键字,您从函数环境外部的函数内部导入变量.
return User::whereHas('roles', function ($query) use ($role) {
return $query->whereId($role);
})->get();
这篇关于将条件参数传递给幼虫的急切加载错误结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文