title: How should I replace a hasMany() relationship with Laravel API Resources for Lumen
body: <p>I'm migrating a Laravel 5.7 app to Lumen, and introducing at the same time <a href="https://laravel.com/docs/5.7/eloquent-resources#conditional-relationships" rel="nofollow noreferrer">Laravel API Resources</a></p>
  
  <p>In my old codebase, I had:</p>
  
  <pre><code>$tournaments = Auth::user()-&gt;tournaments();
  </code></pre>
  
  <p>With </p>
  
  <pre><code>public function tournaments()
  {
      return $this-&gt;hasMany('App\Tournament');
  }
  </code></pre>
  
  <p>But now, in Lumen, I use API Resources, so don't know how to get the same result, but with all the decorated extra fields that provide API resource.</p>
  
  <p>I have: </p>
  
  <pre><code>class TournamentResource 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,
              'name' =&gt; $this-&gt;name,
              'user' =&gt; User::findOrFail(Auth::user()-&gt;id)-&gt;email,
              'championships' =&gt; ChampionshipResource::collection($this-&gt;whenLoaded('championships')),
              'competitors_count' =&gt; $this-&gt;competitors-&gt;count()
          ];
      }
  }
  </code></pre>
  
  <p>Any Idea?</p>