// Product model
public function stockValueSum()
{
return $this->hasMany('Stock')->sum('value');
}
then
Product::find($id)->stockValueSum();
will return sum of related 'value' columns on 'Stock' model's table
Mind though that you can't eager load this value with the above method. For doing this you would have to do some join
$product = Product::with(array('stock' => function($query) {
$query->sum('value');
}))->where('id', '=', $id)->first();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community