0

I developed following code,

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;

}

But I got undefined index $sip_id in ['agent', '=', $sip_id] line. How to pass $sip_id in to function? I have no idea.

4 Answers4

1

Closures needs importing variables from outside.

->where(function ($query) use ($sip_id) {
Laraleg
  • 519
  • 3
  • 7
0

You should try this:

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;

    }
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
0

If you'd rather use closures then you can import variable to the current scope (the use keyword):

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;

}
Guja1501
  • 999
  • 7
  • 16
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;  
}
Surabhi
  • 179
  • 1
  • 6