2

What I am doing ?

I pass CategoryID parameter value as default value to the where clause

public function SubCategories($CategoryID)
{
    $CandSubCat = \Cache::rememberForever('CandSubCat' . $CategoryID, function() {
        return \App\Models\Skill\Category_Model
            ::where("CategoryID", "=", $CategoryID)
            ->with("SubCategories")
            ->first();
    });

    return view("Skill.SubCategory.List", array("CandSubCat" => $CandSubCat));
}

Question:

Why it gives runtime error, saying:

Undefined variable: CategoryID in Where Clause?

Gino Pane
  • 4,740
  • 5
  • 31
  • 46
  • 1
    You are using anonymous function as second parameter. This function has not global scope, you must pass a variable to it. – Gino Pane Dec 18 '15 at 13:21

1 Answers1

3

You should pass the variable to the scope of function with the use keyword like this:

public function SubCategories($CategoryID)
{
    $CandSubCat = \Cache::rememberForever('CandSubCat' . $CategoryID, function() use($CategoryID) {
        return \App\Models\Skill\Category_Model
            ::where("CategoryID", "=", $CategoryID)
            ->with("SubCategories")
            ->first();
    });

    return view("Skill.SubCategory.List", array("CandSubCat" => $CandSubCat));
}
Pantelis Peslis
  • 14,930
  • 5
  • 44
  • 45