简体   繁体   中英

Laravel query builder not using variable from get method

I have this function in one of my controller.

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

Though have passed two variables from get method. But i am getting error of

Undefined variable: country

You are missing use in anonymous function so your query willl be as:

$tour = Tour::whereHas('country', function($q) use($country) {
                $q->where('name','=', $country);
            })
            ->whereHas('category', function($r) use($category) {
                $r->where('name','=', $category);
            })
            ->get();

Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM