title: laravel get relation hasMany model with where clause
body: <p>I have two models:</p>
  
  <p><strong>Playlist</strong> and <strong>Image</strong></p>
  
  <p>a playlist can have many images.
  One image belongs to a playlist.</p>
  
  <p>In table images I create also a column called "position" which can contains 
  integers.</p>
  
  <p>in Playlist.php:</p>
  
  <pre><code>public function images(){
      return $this-&gt;hasMany('Image');
  }
  </code></pre>
  
  <p>in Image.php:</p>
  
  <pre><code>public function playlist(){
      return $this-&gt;belongsTo('Playlist');
  }
  </code></pre>
  
  <p>I select a playlist Model:</p>
  
  <pre><code>$playlist = Playlist::with('images')-&gt;find($id);
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>How can I select an image from my $playlist with a specific position, per example with position 1?
  I want load all images in the controller (to have all images of all positions in view) but in the view I want select a image with a specific position.</p>
  
  <p>With:</p>
  
  <pre><code>$playlist-&gt;images
  </code></pre>
  
  <p>I get ALL the related images, but I want ONLY the image which has a "1" in the position column.</p>
  
  <p>EDIT:</p>
  
  <p>I got this solution:</p>
  
  <p>in Playlist I create a new function:</p>
  
  <pre><code>public function imagePosition($position) {
      return $this-&gt;hasMany('Image')-&gt;wherePosition($position)-&gt;first();
  }
  </code></pre>
  
  <p>And now I can select this image in my view with:</p>
  
  <pre><code>{{$playlist-&gt;imagePosition(1)-&gt;id}}
  </code></pre>
  
  <p>Is this the best way?</p>