【发布时间】:2014-08-05 01:49:25
【问题描述】:
我在 php (laravel) 中有这个功能:
public static function isUserParticipatesInTournament($tourId, $userId)
{
var_dump($userId); //dumped
$user = User::find($userId);
if(!$user)
{
return null;
}
$obj = $user->whereHas('tournaments', function($query)
{
var_dump($tourId); //error
$query->where('id', '=', $tourId); //error
})->get();
return $obj;
}
问题是在闭包$obj = $user->whereHas('tournaments', function($query){...} 中$tourId 变量是未定义的。我收到此错误:
Undefined variable: userId.
为什么会这样?该变量是在内部函数的范围内声明的。我唯一的想法是,它是一个回调函数。
当我试图执行这个函数时:$obj = $user->whereHas('tournaments', function($query, $tourId){...} 然后我得到这个异常:
Missing argument 2 for User::{closure}()
【问题讨论】: