title: Using anonymous PHP function as variable in Laravel callback
body: <p>I'm running the following code in Laravel to load various relationships on my <code>Product</code> model:</p>
  
  <pre><code>$product-&gt;load([
          'skus' =&gt; function($query){
  
                  $query-&gt;select($this-&gt;skuFields)-&gt;with([
  
                          'uniqueItem' =&gt; function($query){
                                  // &lt;----------reuse the code below---------&gt;
                                  $query-&gt;with([
                                          'fulfillmentCenterUniqueItems',
                                          'products' =&gt; function($query){
  
                                                  $query-&gt;select($this-&gt;productFields)-&gt;with([
                                                          'skus' =&gt; function($query){
                                                                  $query-&gt;select($this-&gt;skuFields);
                                                          }
                                                  ]);
                                          },
                                          'skus' =&gt; function($query){
                                                  $query-&gt;select($this-&gt;skuFields);
                                          }
                                  ]);
                                 // &lt;----------reuse the code above---------&gt;
                          }
                  ]);
          },
          'uniqueItem' =&gt; function($query) {
  
                  //need to reuse code here
          },
  ]);
  </code></pre>
  
  <p>As you can see from my note in the code, there is a place where I would like to reuse some code, so I was hoping to place it in a function, and reuse it.</p>
  
  <p>Therefore, I did the following:</p>
  
  <pre><code>$uniqueItemLoadFunction = function($query)
  {
  
          $query-&gt;with([
                  'fulfillmentCenterUniqueItems',
                  'products' =&gt; function($query){
  
                          $query-&gt;select($this-&gt;productFields)-&gt;with([
                                  'skus' =&gt; function($query){
                                          $query-&gt;select($this-&gt;skuFields);
                                  }
                          ]);
                  },
                  'skus' =&gt; function($query){
                          $query-&gt;select($this-&gt;skuFields);
                  }
          ]);
  };
  
  $product-&gt;load([
              'skus' =&gt; function($query) use ($uniqueItemLoadFunction){
  
                      $query-&gt;select($this-&gt;skuFields)-&gt;with([
  
                              'uniqueItem' =&gt; $uniqueItemLoadFunction($query)
                      ]);
              },
              'uniqueItem' =&gt; function($query) {
  
                      //need to reuse code here
              },
      ]);
  </code></pre>
  
  <p>However, I now receive a <code>BadMethodCallException</code>:</p>
  
  <pre><code>Call to undefined method Illuminate\\Database\\Query\\Builder::fulfillmentCenterUniqueItems()
  </code></pre>
  
  <p>This error was not occurring when the code was run the original way. This makes me think I am not using an anonymous function correctly.  How can I make this work?</p>