gpt4 book ai didi

sql - Laravel总和 Eloquent 集合

转载 作者:行者123 更新时间:2023-12-04 14:12:07 24 4
gpt4 key购买 nike

我该如何总结一个渴望加载的数据集?
这是我的表结构:

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');
}

}
这是我的 Controller
  public function index()
{
$regions = Region::with('submits')->get();

return $regions;
//return View::make('pages.index', compact('regions'));
}
这是返回的数据的样子(以json格式,我理解 Eloquent 集合中的$ regions):-

我不知道如何将“交易”(4)的总和发送回 View ?

最佳答案

$deals = $regions->sum(function ($region) {
return $region->submits->sum('deals');
});

关于sql - Laravel总和 Eloquent 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27803573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com