title: Laravel Error when referencing a resource in a resource
body: <p>I'm using a Laravel Json Resource in my controller, as follows</p>
  
  <pre><code>public function index(Request $request)
  {
      $itemsWithTranslations = MenuItem::where(['menu_id' =&gt; $request-&gt;id, 'parent_id' =&gt; null])
          -&gt;with(['children', 'translations'])
          -&gt;orderBy('sort_order', 'asc')
          -&gt;get();
  
      return MenuItemResource::collection($itemsWithTranslations);
  }
  </code></pre>
  
  <p>Now I would like to generate a collection, inside this collection with the children for the item that's being shown.</p>
  
  <p>The following code works fine. <strong>Notice how I commented out the children reference</strong></p>
  
  <pre><code>class MenuItemResource extends JsonResource
  {
  
      /**
       * Transform the resource into an array.
       *
       * @param  \Illuminate\Http\Request $request
       * @return array
       */
      public function toArray($request)
      {
          return [
              'id' =&gt; $this-&gt;id,
              'text' =&gt; $this-&gt;title,
              // 'children' =&gt; MenuItemResource::collection($this-&gt;whenLoaded('children')),
              'data' =&gt; [
                  'id' =&gt; [
                      'value' =&gt; $this-&gt;id,
                      'type' =&gt; 'hidden'
                  ],
                  'title' =&gt; [
                      'value' =&gt; $this-&gt;title,
                      'type' =&gt; 'text',
                      'label' =&gt; 'Title'
                  ],
                  'resource_link' =&gt; [
                      'value' =&gt; $this-&gt;resource_link,
                      'type' =&gt; 'text',
                      'label' =&gt; 'Resource Link'
                  ],
                  'translations' =&gt; MenuItemTranslationResource::collection($this-&gt;whenLoaded('translations'))-&gt;keyBy(function ($translation) {
                      return $translation['locale'];
                  })
              ]
          ];
      }
  }
  </code></pre>
  
  <p>When I uncomment the children, I get the following error</p>
  
  <blockquote>
    <p>"Call to undefined method Illuminate\Http\Resources\Json\AnonymousResourceCollection::keyBy()"</p>
  </blockquote>
  
  <p>Is it wrong, to include a Resource inside a resource? Or how should I go about this?</p>
  
  <h3>Model</h3>
  
  <pre><code>class MenuItem extends Model
  {
      protected $table = 'menu_items';
      protected $fillable = ['menu_id', 'parent_id', 'title', 'order', 'resource_link', 'html_class', 'is_blank'];
  
      public function translations()
      {
          return $this-&gt;hasMany(MenuItemTranslation::class, 'menu_item_id');
      }
  
      public function children()
      {
          return $this-&gt;hasMany(MenuItem::class, 'parent_id');
      }
  }
  </code></pre>
  
  <h3>Extra Information</h3>
  
  <p>When I return the following data, it does return empty as a collection for the children.</p>
  
  <pre><code>MenuItemResource::collection($this-&gt;children);
  </code></pre>
  
  <p>This returns</p>
  
  <p><a href="https://i.stack.imgur.com/HaMMg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HaMMg.png" alt="enter image description here"></a></p>
  
  <p>While if I return the children without a collection, it returns them (for 1 item, which is correct)</p>
  
  <pre><code>return $this-&gt;children;
  </code></pre>
  
  <p>returns</p>
  
  <p><a href="https://i.stack.imgur.com/88DKf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/88DKf.png" alt="enter image description here"></a></p>