如何从相关表中求和函数值?

时间:2018-05-18 19:01:45

标签: php laravel function eloquent sum

我有两个课程:public void Main() { const int a = 1; const int b = 3; var t1 = Task.Run(() => new { c = a + 5 } ); var t2 = t1.ContinueWith(t => new { c = t.Result.c, d = b + 3 } ); var t3 = t2.ContinueWith(t => t.Result.c + t.Result.d ); var result = t3.Result; } 和相关的Share

我在EquityGrant

中有这个数学函数
EquityGrant

但是现在我需要返回public function share() //relationship { return $this->belongsTo(Share::class, 'share_id'); } public function capitalCommitted() //my math function { return $this->share_price * $this->shares_amount; } SharecapitalCommitted()函数的总和,这里我被卡住了。

我的Share课程:

public function grants() //relationship
{
    return $this->hasMany(EquityGrant::class);
}
public function totalIssued() //sum, works good
{
    return $this->grants->sum('shares_amount');
}
public function totalCommitted() //Stuck here
{
    //need help here, Must be like this: array_sum($this->grants->capitalCommitted());
}

1 个答案:

答案 0 :(得分:2)

使用此:

public function getCapitalCommittedAttribute()
{
    return $this->share_price * $this->shares_amount;
}

public function totalCommitted()
{
    return $this->grants->sum('capitalCommitted');
}