5

Attached is a screenshot of the error I am facing. I have the log show the value of the $team variable. What am I doing wrong? enter image description here

4
  • Show your code?? Commented Jul 7, 2018 at 10:27
  • 2
    It should be function($query) use ($team) (string: 18) to import this variable into closure from parent scope Commented Jul 7, 2018 at 10:27
  • Thanks Dan. You were the first to answer, but since it is a comment, I cant upvote you. Commented Jul 7, 2018 at 10:42
  • You might also find using a good IDE like PhpStorm help debug before you test. If you use PhpStorm for example you'd have seen this mistake. Commented Jul 7, 2018 at 10:48

2 Answers 2

10

You don't have access to parents variables from a closure, you need to explicitly send variables to the closure with use keyword

function($query) use ($team)
Sign up to request clarification or add additional context in comments.

Comments

4

Use forget to use $team variable inside your closure. It must be function($query) use ($team)

$data = User::whereHas('roles' => function($query) use ($team) {
    $query->where('role_code', '=', $team)
})->get();

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.