title: PHP error with variable in callback function
body: <p>I have this function in php (laravel):</p>
  
  <pre><code>    public static function isUserParticipatesInTournament($tourId, $userId)
      {
          var_dump($userId); //dumped
          $user = User::find($userId);
  
          if(!$user)
          {
              return null;
          }
  
          $obj = $user-&gt;whereHas('tournaments', function($query)
          {
              var_dump($tourId); //error
              $query-&gt;where('id', '=', $tourId); //error
          })-&gt;get();
  
          return $obj;
      }
  </code></pre>
  
  <p>The problem is that in the closure <code>$obj = $user-&gt;whereHas('tournaments', function($query){...}</code> the <code>$tourId</code> variable is undefined in it. I am getting this error:
  <code>Undefined variable: userId</code>.</p>
  
  <p>Why this is happening? The variable is declared in the scope of the inner function. My only thought is that, that it is a callback function. </p>
  
  <p>When I tried to execute this function : <code>$obj = $user-&gt;whereHas('tournaments', function($query, $tourId){...}</code> then I am getting this exception:</p>
  
  <pre><code>Missing argument 2 for User::{closure}()
  </code></pre>