我开发了以下代码,
public function getInCalls($sip_id){
$resultsTotalInCalls = DB::table('xxxx')
->whereDate('created', '=', date('Y-m-d'))
->where(function ($query) {
$query->where([
['event', '=', 'ENTERQUEUE'],
['agent', '=', $sip_id]
]);
})
->get();
$numberofInCalls = count($resultsTotalInCalls);
return $numberofInCalls;
}
但我在['agent', '=', $sip_id]
行中得到了未定义的索引$ sip_id。如何将$ sip_id传递给函数?我不知道。
答案 0 :(得分:1)
闭包需要从外部导入变量。
->where(function ($query) use ($sip_id) {
答案 1 :(得分:0)
你应该试试这个:
public function getInCalls($sip_id){
$resultsTotalInCalls = DB::table('xxxx')
->whereDate('created', '=', date('Y-m-d'))
->where(function ($query) use($sip_id) {
$query->where([
['event', '=', 'ENTERQUEUE'],
['agent', '=', $sip_id]
]);
})
->get();
$numberofInCalls = count($resultsTotalInCalls);
return $numberofInCalls;
}
答案 2 :(得分:0)
如果您更喜欢使用闭包,那么您可以将变量导入当前范围(
use
关键字):
https://stackoverflow.com/a/11086796/3520693
public function getInCalls($sip_id){
$resultsTotalInCalls = DB::table('xxxx')
->whereDate('created', '=', date('Y-m-d'))
->where(function ($query) use ($sip_id) {
$query->where([
['event', '=', 'ENTERQUEUE'],
['agent', '=', $sip_id]
]);
})
->get();
$numberofInCalls = count($resultsTotalInCalls);
return $numberofInCalls;
}
答案 3 :(得分:0)
public function getInCalls($sip_id){
$resultsTotalInCalls = DB::table('abc')
->whereDate('created', '=', date('Y-m-d'))
->where(function ($query) use ($sip_id) {
$query->where([ ['event', '=', 'ENTERQUEUE'], ['agent', '=', $sip_id] ]);
})
->get();
$numberofInCalls = count($resultsTotalInCalls);
return $numberofInCalls;
}