【发布时间】:2020-09-25 18:00:13
【问题描述】:
我的数据库表是
===batches ===
id
batch_title
===courses===
id
course_title
batch_id
===students ===
id
name
===course_student===
course_id
student_id
现在我想显示有多少学生加入了这个批次,还有多少学生参加了这个课程
我的学生模特
public function courses()
{
return $this->belongsToMany('App\Course');
}
课程模型
public function students(){
return $this->hasManyThrough('App\Course','App\Student');
}
public function batch() {
return $this->belongsTo('App\Batch','batch_id');
}
批处理模型
public function courses(){
return $this->hasOne('App\Course','batch_id');
}
【问题讨论】:
标签: laravel eloquent orm relationship