Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
回调函数中包含变量的PHP错误_Php_Laravel_Scope - Fatal编程技术网

回调函数中包含变量的PHP错误

回调函数中包含变量的PHP错误,php,laravel,scope,Php,Laravel,Scope,我在php(laravel)中有这个函数: 问题在于,在闭包$obj=$user->whereHas('tournaments',函数($query){…}中,$tourId变量未定义。我收到以下错误: 未定义变量:userId 为什么会发生这种情况?变量是在内部函数的范围内声明的。我唯一的想法是,它是一个回调函数 当我尝试执行此函数:$obj=$user->whereHas('tournaments',函数($query,$tourId){…}时,我遇到了以下异常: Missing argum

我在php(laravel)中有这个函数:

问题在于,在闭包
$obj=$user->whereHas('tournaments',函数($query){…}
中,
$tourId
变量未定义。我收到以下错误:
未定义变量:userId

为什么会发生这种情况?变量是在内部函数的范围内声明的。我唯一的想法是,它是一个回调函数

当我尝试执行此函数:
$obj=$user->whereHas('tournaments',函数($query,$tourId){…}
时,我遇到了以下异常:

Missing argument 2 for User::{closure}()

您的
$tourId
变量不在匿名函数的作用域中。请查看
使用
关键字以了解如何将其添加到作用域中。请参阅本页的示例3:

它应该如下所示:

$obj = $user->whereHas('tournaments', function($query) use($tourId)
    {
        var_dump($tourId); // Dumps OK
    })->get();
$obj = $user->whereHas('tournaments', function($query) use($tourId)
    {
        var_dump($tourId); // Dumps OK
    })->get();