我有两张表:
LECTURES
id name
1 - Lesson 1
2 - Lesson 2
3 - Lesson 3
LECTURESTYPE
id id_lectures numb
1 - 1 - 10
2 - 2 - 20
3 - 1 - 30如何对每一课数字列求和并取得如下结果:名称:第1课数字: 40
我想要显示每一课数字总和。
发布于 2015-10-22 23:46:18
试试LECTURESTYPE::with('lectures')->sum('numb')->GroupBy('id_lectures')->get()
发布于 2015-10-23 00:15:48
您需要以下关系:
Lecture (App\Lecture.php)
namespace App;
class Lecture extends Model {
protected $table = 'lectures';
public function lecturetype() {
return $this->hasMany('App\LectureType');
}
}LectureType (App\LectureType.php)
namespace App;
class LectureType extends Model {
protected $table = 'lecturestype';
public function lecture() {
return $this->belongsTo('App\Lecture');
}
}现在,您可以在控制器中尝试:
$sum = App\LectureType::with('lecture')->GroupBy('id_lectures')->sum('numb');发布于 2015-10-23 00:21:26
试试LECTURESTYPE::with('lectures')->GroupBy('id_lectures')->sum('numb')
https://stackoverflow.com/questions/33284520
复制相似问题