我将CategoryID
参数值作为默认值传递给where
子句
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));
}
为什么它会给运行时错误,说:
未定义的变量:Where子句中的CategoryID?
答案 0 :(得分:2)
您应该使用use
关键字将变量传递到函数范围,如下所示:
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));
}