如何从关联表中汇总自定义函数的计算值?
我有两个类:Share和关联的EquityGrant。在EquityGrant类中定义了如下关联与计算函数:
public function share() // 关联关系 { return $this->belongsTo(Share::class, 'share_id'); } public function capitalCommitted() // 自定义计算函数 { return $this->share_price * $this->shares_amount; }
现在需要在Share类中汇总所有关联EquityGrant的capitalCommitted()函数返回值,目前卡在totalCommitted()方法的实现上。我的Share类代码如下:
public function grants() // 关联关系 { return $this->hasMany(EquityGrant::class); } public function totalIssued() // 汇总字段,已经正常工作 { return $this->grants->sum('shares_amount'); } public function totalCommitted() // 卡在这里了 { // 需要实现类似这样的逻辑:array_sum($this->grants->capitalCommitted()); }
解决方案
直接调用sum('capitalCommitted')是行不通的,因为capitalCommitted不是数据库字段,而是你定义的模型方法。不过这里有两种实用的实现方式,你可以根据业务场景选择:
方案一:利用集合闭包求和(适合小数据量)
这种方式会先把所有关联的EquityGrant实例加载到内存中,再遍历每个实例调用计算方法求和,代码直观易读:
public function totalCommitted() { return $this->grants->sum(function ($grant) { return $grant->capitalCommitted(); }); }
如果你的PHP版本支持箭头函数(PHP 7.4及以上),还可以简化成:
public function totalCommitted() { return $this->grants->sum(fn($grant) => $grant->capitalCommitted()); }
方案二:数据库层面直接计算(适合大数据量,性能更优)
如果关联的数据量较大,方案一可能因加载过多模型实例导致性能问题,这时候可以直接让数据库完成计算,无需加载所有模型:
public function totalCommitted() { return $this->grants() ->selectRaw('SUM(share_price * shares_amount) as total_committed') ->value('total_committed'); }
这种方式直接在数据库层面执行求和逻辑,返回结果,效率更高,还能避免潜在的N+1查询问题。
你可以根据自己的业务规模和性能需求选择合适的方案~
内容的提问来源于stack exchange,提问作者Philipp Kishkovarov
相关产品推荐
相关产品推荐

