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

blntgvn42's avatar

Filament Multiple Many to Many Relation Resource Management

Hello,

I have been working on a school management project. My models are Semester, Classroom & Student. Each semester can have multiple class and each class can be open for teaching for any semester. Since it is one-level-deep many-to-many relation, I have managed to complete this filament relation. However, every classroom has also many student. I first create semester then attach some classes but I also want to attach student to this classroom that is attached to the semester. This phase is what i have problem creating. I thought about multi-tenancy but my requirements is not meet for it.

I am open to any suggestion to improve myself in this topic. Thank you in advance. If you have any question about any clarity, feel free to ask.

My tables are;

Schema::create('semesters', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamp('start_date');
            $table->timestamp('end_date');
            $table->timestamps();
            $table->softDeletes();
});	
Schema::create('classrooms', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('code');
            $table->foreignId('responsible_personnel_id')->constrained('personnels')->cascadeOnDelete();
            $table->foreignId('reserve_responsible_personnel_id')->constrained('personnels')->cascadeOnDelete();
            $table->integer('floor');
            $table->string('responsible_parent_name');
            $table->string('responsible_parent_phone');
            $table->timestamps();
            $table->softDeletes();
});
Schema::create('classroom_semester', function (Blueprint $table) {
            $table->id();
            $table->foreignId('semester_id')->constrained()->cascadeOnDelete();
            $table->foreignId('classroom_id')->constrained()->cascadeOnDelete();
            $table->timestamps();
});
Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('surname');
            $table->string('code');
            $table->timestamp('birth_date');
            $table->string('mother_name');
            $table->string('father_name');
            $table->longText('address');
            $table->string('phone_1');
            $table->string('phone_2')->nullable();
            $table->string('phone_emergency')->nullable();
            $table->string('city');
            $table->string('district');
            $table->string('education_type')->default(EducationType::FULL_TIME);
            $table->timestamps();
            $table->softDeletes();
});
Schema::create('classroom_student', function (Blueprint $table) {
            $table->id();
            $table->foreignId('classroom_id')->constrained()->cascadeOnDelete();
            $table->foreignId('student_id')->constrained()->cascadeOnDelete();
            $table->timestamps();
});

My models are

Semester.php

public function classrooms()
    {
        return $this->belongsToMany(Classroom::class)->withTimestamps();
    }

Classroom.php

public function semesters()
    {
        return $this->belongsToMany(Semester::class)->withTimestamps();
    }

    public function students()
    {
        return $this->belongsToMany(Student::class)->withTimestamps();
    }

Student.php

public function classrooms()
    {
        return $this->belongsToMany(Classroom::class)->withTimestamps();
    }
0 likes
3 replies
El Klo's avatar

Why don't you just simply create a relation manager on the classrooms to attach/detach students? It looks like all the relationship are already there.

1 like
blntgvn42's avatar

Hello,

Thank you for your answer. I have create

php artisan make:filament-relation-manager Semester classrooms name

php artisan make:filament-relation-manager Classroom students name

these two works perfectly fine.

I am able to attach classrooms to semester. However, I have problem solving attaching student to classroom that is defined in the classroom semester.

To clarify,

Let's say;

I have two classroom called 10A & 10B and two semester Semester1 & Semester2.

I am able to attact 10A & 10B to Semester1 and 10A to Semester1.

But when I want to attach StudentA to 10A in Semester1, it also attaches 10A in Semester2.

Exactly that point is where I have confusion.

Probably, what I need is the relationship between classroom_semester and student.

But I have never done such relationship with laravel and filament (I think I need help here :) )

Please or to participate in this conversation.