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

idodeva's avatar

Many to many - Relational database for students/courses

Hi all,

I am trying to build an app for university students.

My idea is to retrieve data from the database, where for each student should be displayed courses depending on which semester the student is. In other words if student A is in second semester and let's say the course Databases is being held only in second semester, for student A I should have on the page only Databases as eligible course.

I have the following tables:

  • users
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}
  • semester
public function up()
{
    Schema::create('semesters', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('number');
        $table->timestamps();
    });
}
  • additional_info_students
public function up()
{
    Schema::create('additional_info_students', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('first_name', 255);
        $table->string('last_name', 255);
        $table->integer('faculty_number')->unique();        
        $table->integer('group_number');
        $table->integer('year');
        $table->integer('semester_id')->unsigned();
        $table->foreign('semester_id')->references('id')->on('semesters');
        $table->timestamps();
    });
}
  • courses
public function up()
{
    Schema::create('courses', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 255);
        $table->string('language', 10);
        $table->longText('description', 30000000);
        $table->integer('semester_id')->unsigned();
        $table->foreign('semester_id')->references('id')->on('semesters');
        $table->timestamps();
    });
}
  • courses_assign_students
public function up()
{
    Schema::create('courses_assign_students', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('student_id')->unsigned();
        $table->foreign('student_id')->references('user_id')->on('additional_info_students');
        $table->integer('courses_id')->unsigned();
        $table->foreign('courses_id')->references('id')->on('courses');
        $table->timestamps();
    });     
}

Would this work? I can not seem to join them correctly in order to get what I need.

Thanks in advance :)

0 likes
1 reply
aurawindsurfing's avatar

In Laravel, you use models to define that relationship and its conditions. Database tables are there only to hold correct data and not to define relationships. Even the keys on database tables could be incorrect as you can define them in the Model itself.

In other words, you should look at the Models and not the database tables so much.

Please or to participate in this conversation.