2

When I try to get all the records that have a relation to the category table, where the category_definition_id is the search input, I cannot access the $request->category variable in the sub method. It is in Laravel 5.5. Any ideas?

public function jobopenings(Request $request)
{
  $category_definitions = Category_definition::all();
  $skill_definitions = Skill_definition::all();

  $ads = new Ad;

  // Search for a user based on their name.
  if ($request->has('category')) {

    $ads = $ads->whereHas('categories', function($query){
        $query->where('category_definition_id', $request->category);
      });
  }

  $ads = $ads->where('type','1')->orderBy('created_at','desc')->paginate(15);

    return View::make('jobs')
    ->with('ads', $ads)
    ->with('category_definitions',$category_definitions)
    ->with('skill_definitions', $skill_definitions)
    ->with('ad_type','1');
}

2 Answers 2

1

You have to declare it using use($request):

$ads = $ads->whereHas('categories', function($query) use ($request) {
           $query->where('category_definition_id', $request->category);
       });
Sign up to request clarification or add additional context in comments.

Comments

0

Add use clause:

$ads = $ads->whereHas('categories', function($query) use ($request) {
        $query->where('category_definition_id', $request->category);
    });
}

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.