title: Laravel Sum of relation
body: <p>I want to return the sum of "amount" from my payments table. There can be many payments for one invoice. The below "->sum('amount') does not work, it returns:</p>
  
  <p>Call to a member function addEagerConstraints() on a non-object.</p>
  
  <p>How to return the sum of all payments for each invoice in my relation?</p>
  
  <p>Invoices Model:</p>
  
  <pre><code>class Invoices extends Eloquent {
  
      public function payments()
      {
          return $this-&gt;hasMany('Payments')-&gt;sum('amount');
      }
  }
  </code></pre>
  
  <p>Expenses Model:</p>
  
  <pre><code>class Payments extends Eloquent {
  
      public function invoices()
      {
          return $this-&gt;belongsTo('Invoices');
      } 
  }
  </code></pre>
  
  <p>My table "payments" holds the foreign key of my tables invoices, which is invoices_id.</p>