If i understood the setup correctly i'd start like this
Lecturers
- id
- name
Subjects
- id
- name
- course_id
Courses
- id
- name
Students
- id
- name
Lecturer_Subject (A lecturer can teach many subject a subject can have many lecturers)
- lecturer_id
- subject_id
Course_Sudent (A student can be enrolled on many courses and a course can have many students)
- course_id
- student_id
Follow the models
class Lecturer extends Model
{
public function subjects()
{
return $this->belongsToMany('App\Subject', 'lecturer_subject', 'lecturer_id', 'subject_id');
}
}
class Subject extends Model
{
public function course()
{
return $this->belongsTo('App\Course', 'course_id');
}
public function lecturer()
{
return $this->belongsToMany('App\Subject', 'lecturer_subject', 'subject_id', 'lecturer_id');
}
}
class Course extends Model
{
public function subjects()
{
return $this->hasMany('App\Subject', 'course_id');
}
public function students()
{
return $this->belongsToMany('App\Student', 'course_student', 'course_id', 'student_id');
}
}
class Student extends Model
{
public function courses()
{
return $this->belongsToMany('App\Course', 'course_student', 'student_id', 'course_id');
}
}
Unless i missed something this should be it. Also i gave more readable names to the fields. All those abbreviations are just confusing in the long run.
1 .
$courses = Course::with('subjects')->find($course_id);
Now for the given course you have available $subjects->name
2.Not necessarily. If the student is enrolled to that course and the course contains that subject you don't need to.
- You could but it's exhausting.
Let me know if it suffices and if you need further assistance :)