3

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
3
  • Show me the routes path @zacharyDale Commented Nov 26, 2016 at 16:09
  • Route::get('tour/{country}/{categpry}', ['as' => 'tour.list', 'uses' => 'PublicController@tourList']); Commented Nov 26, 2016 at 16:18
  • Change '$tour' to $tour as we have to pass variable in withTour. Commented Nov 26, 2016 at 16:21

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.