我在我的一个控制器中有这个功能。
public function tourList($country, $category)
{
$tour = Tour::whereHas('country', function($q) {
$q->where('name','=', $country);
})
->whereHas('category', function($r) {
$r->where('name','=', $category);
})
->get();
return view('tour-list.blade.php')->withTour('$tour');
}
虽然已经从get方法传递了两个变量。但是我得到了
的错误Undefined variable: country
答案 0 :(得分:3)
您在匿名函数中缺少use
,因此您的查询将如下:
$tour = Tour::whereHas('country', function($q) use($country) {
$q->where('name','=', $country);
})
->whereHas('category', function($r) use($category) {
$r->where('name','=', $category);
})
->get();