title: Laravel undefined variable inside a function
body: <p>I get my <code>$id</code> from the url, but I couldn't find a way to use it in a function. How can I pass $id to the query function?</p>
  
  <p>I tried global $id but I still can't get a value, just empty page.</p>
  
  <p>My controller.</p>
  
  <pre><code>class BookController extends Controller
  {
      public function bookIndex($id, $slug)
      {
       // echo $id works without a problem
       $findcover = Thing::with(array('cover' =&gt; function($query) {
                  $query-&gt;where('imageable_type', 'App\Models\Thing')
                    -&gt;where('imageable_id', $id);
              }))-&gt;find($id);
  
              $maincover = $findcover-&gt;cover;
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>ErrorException in BookController.php line 49:
  Undefined variable: id
  </code></pre>
  
  <p>Line 49</p>
  
  <pre><code>-&gt;where('imageable_id', $id);
  </code></pre>