0
public function showMatch($id){
    $bat_perfo = Cache::remember('bat_1_'.$id, 2, function(){
        return Bat::whereRaw('match_id = ? AND inning = 1', array($id))->get();
    });
    return 0;
}

I am getting the error "Undefined variable: id" on line 3

how do i solve this

Akshay Naik
  • 104
  • 6

2 Answers2

2

See Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?.

The function introduces a new scope, and $id is not in scope inside the function. Use use to extend its scope:

public function showMatch($id){
    $bat_perfo = Cache::remember('bat_1_'.$id, 2, function () use ($id) {
        return Bat::whereRaw('match_id = ? AND inning = 1', array($id))->get();
    });
    return 0;
}
Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
1

Add "use" keyword

public function showMatch($id){
    $bat_perfo = Cache::remember('bat_1_'.$id, 2, function() use ($id){
        return Bat::whereRaw('match_id = ? AND inning = 1', array($id))->get();
    });
    return 0;
}
michal.hubczyk
  • 698
  • 3
  • 9