title: Eloquent Laravel - Retrieve results only if the relationship exists
body: <p>I am trying to retrieve a list of genres which have a track assigned to them, I am using a whereHas eloquent query but feel a little lost. </p>
  
  <p>I have the following</p>
  
  <pre><code>//model
  public function workingGenre() {
      $this-&gt;whereHas('tracks', function($query) {
          $query-&gt;where('');
      });
  
      return $this-&gt;tracks()-&gt;first();
  }
  
  //controller (where i am passing variables etc)
  $genres = Genre::with('tracks')-&gt;get();
  
  //my view
  @foreach($genres as $genre)
     {{print_r($genre-&gt;workingGenre())}}
  @endforeach
  </code></pre>
  
  <p>I'm aware that my Where is empty, currently my print_r returns the following: </p>
  
  <pre><code>                                        1
                                          App\Track Object
   (
  [table:protected] =&gt; Track
  [fillable:protected] =&gt; Array
      (
          [0] =&gt; id
          [1] =&gt; GenreId
          [2] =&gt; Title
          [3] =&gt; CreatedById
          [4] =&gt; SelectedCount
          [5] =&gt; ProducerName
          [6] =&gt; SourceId
          [7] =&gt; Published
      )
  
  [connection:protected] =&gt; 
  [primaryKey:protected] =&gt; id
  [perPage:protected] =&gt; 15
  [incrementing] =&gt; 1
  [timestamps] =&gt; 1
  [attributes:protected] =&gt; Array
      (
          [id] =&gt; 6
          [GenreId] =&gt; 4
          [Title] =&gt; Guns Up
          [CreatedById] =&gt; 1
          [SelectedCount] =&gt; 0
          [ProducerName] =&gt; 
          [SourceId] =&gt; 1
          [Published] =&gt; 1
      )
  
  [original:protected] =&gt; Array
      (
          [id] =&gt; 6
          [GenreId] =&gt; 4
          [Title] =&gt; Guns Up
          [CreatedById] =&gt; 1
          [SelectedCount] =&gt; 0
          [ProducerName] =&gt; 
          [SourceId] =&gt; 1
          [Published] =&gt; 1
      )
  
  [relations:protected] =&gt; Array
      (
      )
  
  [hidden:protected] =&gt; Array
      (
      )
  
  [visible:protected] =&gt; Array
      (
      )
  
  [appends:protected] =&gt; Array
      (
      )
  
  [guarded:protected] =&gt; Array
      (
          [0] =&gt; *
      )
  
  [dates:protected] =&gt; Array
      (
      )
  
  [dateFormat:protected] =&gt; 
  [casts:protected] =&gt; Array
      (
      )
  
  [touches:protected] =&gt; Array
      (
      )
  
  [observables:protected] =&gt; Array
      (
      )
  
  [with:protected] =&gt; Array
      (
      )
  
  [morphClass:protected] =&gt; 
  [exists] =&gt; 1
  [wasRecentlyCreated] =&gt; 
  )
  
  **And so on**
  </code></pre>
  
  <p>A push in the right direction would be great, I am finding this whole eloquent stuff really useful but hard to wrap my head around.</p>
  
  <p>Thanks</p>