title: Laravel model method with default value
body: <p>There are two tables</p>
  
  <ul>
  <li>products </li>
  <li>pictures ( has product_id , priority)</li>
  </ul>
  
  <p>In my <strong>Product model</strong> I can get all pictures with:</p>
  
  <pre><code>public function pictures(){
      return $this-&gt;hasMany('App\Picture');
  }
  </code></pre>
  
  <p>Now i want to create a <strong>method in product</strong> to get the main pictures url (lowest priority value with product_id of picture), and if there is no result return a default value. I tried:</p>
  
  <pre><code>public function mainpicture(){
      $picture = $this-&gt;pictures()-&gt;orderBy('priority','desc')-&gt;first();
      if($picture == null)
          return 'default/default.png';
      return $picture-&gt;url;
  }
  </code></pre>
  
  <p>This gives the error:</p>
  
  <blockquote>
    <p>Relationship method must return an object of type
    Illuminate\Database\Eloquent\Relations\Relation</p>
  </blockquote>
  
  <p>When returning a value it's never of the type  Illuminate\Database\Eloquent\Relations\Relation but just an url.
  How can I make a method to get the mainpicture or a default value?  </p>
  
  <p><em>The reason I want to create this function is: because I need it in alot of views and don't want to paste the same code in every view. 
  Currently in all the views where I need the mainpicture I use:</em></p>
  
  <pre><code>@if(isset($product-&gt;pictures[0]))
      {!! Html::image('/images/product/'.$product-&gt;id.'/thumb/'.pictures[0]-&gt;url, 'Product image', array('class'=&gt; ' img-responsive')) !!}
  @else
      {!! Html::image('/images/product/default/default.png', 'No picture found', array('class'=&gt; ' img-responsive')) !!}
  @endif
  </code></pre>
  
  <p><em>This is very spaghetti like and a function would be much better.</em> </p>
  
  <p>I could create a global helper function, but im curious if i could add it to my model.</p>