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

1
  • our you could pass the $id to the callback function using ... 'bat_1_'.$id, 2, function($id){ ... Commented Mar 17, 2014 at 10:26

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.