我有两张桌子:
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
我想为每节课显示麻木的总和。
答案 0 :(得分:0)
尝试LECTURESTYPE::with('lectures')->sum('numb')->GroupBy('id_lectures')->get()
答案 1 :(得分:0)
您需要以下关系:
讲座(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');
}
}
现在您可以尝试使用Controller:
$sum = App\LectureType::with('lecture')->GroupBy('id_lectures')->sum('numb');
答案 2 :(得分:0)
尝试LECTURESTYPE::with('lectures')->GroupBy('id_lectures')->sum('numb')