ERD : enter image description here
Model
Student
: idCourse
: idStudent_Course
(payment) : id, student_id, course_idThe relation between Student
and Course
is Many to Many, because of that I make another 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.
can you please try following example:
Model/Student.php //write relationship in your model
public function courses()
{
return $this->belongsToMany(Course::class, 'payment');
}
and then try with following eloquent query
$students = Student::whereHas('courses')
->take(10)
->get();