我无法使用变量过滤集合:
$sections = Section::all();
$courses = array("MATH282", "MATH201" , "GEOM202");
foreach ($courses as $course)
{
$sections1 = $sections->filter(function($foo)
{
if ($foo->course == $course ) {
return true;
}
});
}
如果条件是字符串,则过滤器有效。其他一切都有效。
答案 0 :(得分:1)
您在过滤器闭包功能中缺少use()
函数。您的代码应为:
$sections = Section::all(); $courses = array("MATH282", "MATH201" , "GEOM202");
foreach ($courses as $course) {
$sections1 = $sections->filter(function($foo) use($course) {
if ($foo->course == $course ) {
return true;
}
});
}