Asked by: Alvian Supriadi
ERD : enter image description here
Model :
- Student : id
- Course : id
- Student_Course (payment) : id, student_id, course_id
The ralation between Student and Course is Many to Many, because of that I make anothe table (Student_Course).
I have one question, that is "Show total amount of students that enroll at least 1 course". Please help me to find the result. I stuck on that.
Answers
Answered by: Malde Chavda at 2017-07-11 11:19AM
can you please try following example:
Model/Student.php //write relationship in your model
public function courses()
{
return $this->belongsToMany(HorseTraining::class, 'payment');
}
and then try with following eloquent query
$students = Student::whereHas('courses')
->take(10)
->get();
Answered by: sachin at 2017-07-11 11:29AM
Try this.
use Illuminate\Database\Eloquent\Model;
class Student extends Model {
public function student_courses() {
return $this->hasMany('App\StudentCourses');
}
}
$students = Student::whereHas('student_courses')->get();
No comments:
Post a Comment