无法访问 Laravel 8 中函数内部的局部变量
Not able to access local variable inside function in Laravel 8
我正在尝试访问函数内的变量。数组变量已在函数外声明。
我的代码:
$job_bookings = DB::table('job_bookings')
->where('client_id','=',$p1Array['incharge_id'])
->where( function($query) {
if( Arr::has($p1Array, 'includeProcessJob') ) {
$query -> where('ndt_job_status_id','=',1)
-> orWhere('ndt_job_status_id','=',2);
} else {
$query -> where('ndt_job_status_id','=',1);
}
})
我在 where
子句函数中执行代码时收到未定义的 $p1Array
变量错误。函数外的代码(->where('client_id','=',$p1Array['incharge_id'])
),没有收到任何错误。
如果有人可以帮助解决这个问题,我们将不胜感激!
你必须在 use() 中传递那个变量(你想使用的)。
喜欢:
->where( function($query) use($p1Array) {}
因此您的查询将是
$job_bookings = DB::table('job_bookings')
->where('client_id','=',$p1Array['incharge_id'])
->where( function($query) use($p1Array) {
if( Arr::has($p1Array, 'includeProcessJob') ) {
$query->where('ndt_job_status_id','=',1)
->orWhere('ndt_job_status_id','=',2);
} else {
$query->where('ndt_job_status_id','=',1);
}
})
我正在尝试访问函数内的变量。数组变量已在函数外声明。
我的代码:
$job_bookings = DB::table('job_bookings')
->where('client_id','=',$p1Array['incharge_id'])
->where( function($query) {
if( Arr::has($p1Array, 'includeProcessJob') ) {
$query -> where('ndt_job_status_id','=',1)
-> orWhere('ndt_job_status_id','=',2);
} else {
$query -> where('ndt_job_status_id','=',1);
}
})
我在 where
子句函数中执行代码时收到未定义的 $p1Array
变量错误。函数外的代码(->where('client_id','=',$p1Array['incharge_id'])
),没有收到任何错误。
如果有人可以帮助解决这个问题,我们将不胜感激!
你必须在 use() 中传递那个变量(你想使用的)。
喜欢:
->where( function($query) use($p1Array) {}
因此您的查询将是
$job_bookings = DB::table('job_bookings')
->where('client_id','=',$p1Array['incharge_id'])
->where( function($query) use($p1Array) {
if( Arr::has($p1Array, 'includeProcessJob') ) {
$query->where('ndt_job_status_id','=',1)
->orWhere('ndt_job_status_id','=',2);
} else {
$query->where('ndt_job_status_id','=',1);
}
})