Sql 拉威尔和雄辩的集合
如何对已加载的数据集求和 这是我的表格结构:Sql 拉威尔和雄辩的集合,sql,laravel,orm,calculated-field,Sql,Laravel,Orm,Calculated Field,如何对已加载的数据集求和 这是我的表格结构: regions table +------------+------------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------+------+-
regions table
+------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| region | varchar(255) | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
+------------+------------------+------+-----+---------------------+----------------+
submits table
+------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| region_id | int(11) | NO | | NULL | |
| deals | int(11) | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
+------------+------------------+------+-----+---------------------+----------------+
以下是我的模型/关系:-
class Region extends Eloquent {
public function submits()
{
return $this->hasMany('Submit');
}
}
<?php
class Submit extends Eloquent {
public function regions()
{
return $this->belongsTo('Region');
}
}
这就是返回的数据的样子(在json格式中,我理解$regions是一个雄辩的集合):-
我想不出如何将“交易”(4)的总数发送回视图?您可以显示这一点
$deals = $regions->sum(function ($region) {
return $region->submits->sum('deals');
});
$invoices=Region::withSum('submits:deals')->get();
$deals = $regions->sum(function ($region) {
return $region->submits->sum('deals');
});