Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afrasiyabhaider's avatar

How to build schema/model of Tables for Student/Course allocation

I want to allocate courses to

  • students
  • semesters
I basically want to make the relation of

    - courses with semester
    - allocate semester to students

so students should have all courses which are taught in the chosen semester

please guide me on this

0 likes
3 replies
afrasiyabhaider's avatar

@malico thank you for your replay but I want to assign course to semester and then allocate semester to the student so a student can have all courses taught in selected semester.

Can you help me in understanding this scenario!

malico's avatar

Migrations

Courses
- id
- semester_id

Semesters
- id 
- ...

Students
- id 
- ...
- semester_id

Models


// App\Student.php
class Student extends Model {

    public function courses()
    {
        return $this->hasManyThrough('App\Semester', 'App\Course');
    }
}

So with that your can query the students courses with

class StudentController extends Controller{
      public function controllerMethod(){
             $student->courses;
     }
}

Based on what you submit @afrasiyabhaider. Also check this out, it might help: https://github.com/staudenmeir/belongs-to-through

Please or to participate in this conversation.