title: Laravel - how to sum() field from relation
body: <p>I'm trying to optimize this code because it's causing Timeout problems.</p>
  <p>It's getting all items from database and performing SUM() in PHP.</p>
  <pre><code>public function getInstalledCost()
  {
    $items = $this-&gt;stockItems()
                                -&gt;where('status', 'INSTALADO')
                                -&gt;with(['shipment' =&gt; function($q){ $q-&gt;select('id', 'item_cost'); }])
                                -&gt;select('id','shipment_id')
                                -&gt;get();
  
    $cost = 0;
    foreach($items as $i){
      $cost += $i-&gt;shipment-&gt;item_cost;
    }
  
    return $cost;
  }
  </code></pre>
  <p>So is there any way to get this data already SUM() from the database?</p>