title: Laravel how to add a custom function in an Eloquent model?
body: <p>I have a Product model</p>
  
  <pre><code>class Product extends Model
  {
      ...
  
      public function prices()
      {
          return $this-&gt;hasMany('App\Price');
      }
  
      ...
  }
  </code></pre>
  
  <p>I want to add a function which will return the lowest price, and in controller I can get the value using:</p>
  
  <pre><code>Product::find(1)-&gt;lowest;
  </code></pre>
  
  <p>I added this in Product model:</p>
  
  <pre><code>public function lowest()
  {
      return $this-&gt;prices-&gt;min('price');
  }
  </code></pre>
  
  <p>but I got an error saying:</p>
  
  <pre><code>Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
  </code></pre>
  
  <p>And if I use <code>Product::find(1)-&gt;lowest();</code>, it will work. Is it possible to get <code>Product::find(1)-&gt;lowest;</code> to work?</p>
  
  <p>Any help would be appreciated.</p>