Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 991
Zachary Dale Avatar asked Jul 04 '26 18:07

Zachary Dale


1 Answers

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

like image 183
Amit Gupta Avatar answered Jul 07 '26 08:07

Amit Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!