Many to many relationships will occur when we’ve 2 tables which are interconnected with each other with multiple records.

Definition: Many-to-many (M:M) – is used to relate many records in the table A with many records in the table B. A record (‘parent’) in Table A can have many matching records (‘children’) in Table B, and a record (‘child’) in Table B can have many matching records (‘parents’) in Table A.

So, now we’ll be understanding about how to create a Many to Many Relationship in Laravel with migration through foreign key references and sync data with pivot table and CRUD operation for students, subjects related to Many to Many Relationship.

Steps are as follows:

      1. At the very first install fresh Laravel and create a database and make environment configurations.
        Terminal Command: composer create-project laravel/laravel test-app
      2. Create 2 Models with migration through below commands:
        Terminal Command: php artisan make:model Student -m

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('student', function (Blueprint $table){
                $table->id();
        
                $table->string('f_name')->nullable();
                $table->string('l_name')->nullable();
                $table->string('address')->nullable();
        
                $table->timestamps();
            });
        }
        
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('student');
        }

        Terminal Command: php artisan make:model Subject -m

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('subject', function (Blueprint $table){
                $table->id();
        
                $table->string('name')->nullable();
        
                $table->timestamps();
            });
        }
        
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('subject');
        }
      3. Create a new pivot table:
        We need to connect subjects, students both tables and which connected through a pivot table. With below terminal command pivot table migration will be created.
        Terminal Command: php artisan make:migration createStudentSubjectPivotTable

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('student_subject', function (Blueprint $table){
                $table->id();
        
                $table->foreignId('student_id')->nullable()->constrained('student')->onUpdate('SET NULL')->onDelete('CASCADE');
                $table->foreignId('subject_id')->nullable()->constrained('subject')->onUpdate('SET NULL')->onDelete('CASCADE');
                $table->string('grade')->nullable();
        
                $table->timestamps();
            });
        }
        
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('student_subject');
        }
      4. Now, both models are defined through foreign references which is as follows:
        Student model will be like this:

        <?php
        
        namespace App\Models;
        
        use Illuminate\Database\Eloquent\Factories\HasFactory;
        use Illuminate\Database\Eloquent\Model;
        
        class Student extends Model
        {
            use HasFactory;
        
            protected $table = "student";
        
            public function subject(){
                return $this->belongsToMany(Subject::class, 'student_subject');
            }
        }

        Subject model will be like this:

        <?php
        
        namespace App\Models;
        
        use Illuminate\Database\Eloquent\Factories\HasFactory;
        use Illuminate\Database\Eloquent\Model;
        
        class Subject extends Model
        {
            use HasFactory;
        
            protected $table = "subject";
        
            public function student(){
                return $this->belongsToMany(Student::class, 'student_subject');
            }
        }
      5. Once we’ve defined the relationship, we can run the migrate to create tables in our database through below command:
        Terminal Command: php artisan migrate
      6. Create/Update Records:
        Next, we need to use helper methods attach(), detach(), sync() which helps us to assign Many to many Relationships. For now temporary purpose I’ve taken fake data for storing records, you can take your own or get it from the form submit also.
        attach() function

        pubilc function store (){
            Subject::create([
                ['name' => 'English'],
                ['name' => 'Mathematics'],
                ['name' => 'Science'],
            ]);
        
            $student_item = Student::create([
                'f_name' => fake()->firstName(),
                'l_name' => fake()->lastName(),
                'address' => fake()->address(),
            ]);
            $student_item->subject()->attach([1, 2, 3]);
        }

        sync() function

        pubilc function update (){
            Subject::create([
                ['name' => 'Drawing'],
                ['name' => 'Social Science'],
            ]);
        
            $student_item = Student::find(1);
            $student_item->subject()->sync([4, 5]);
        }

        detach() function

        Code:
        
        pubilc function destroy (){
            $student_item = Student::find(1);
            $student_item->subject()->detach();
            $student_item->delete();
        }
      7. Retrieve Records:
        pubilc function index (){
            $student_list = Student::with('subject')->get();
            return view ('student', compact('student_list'));
        }

        Same way we can define a single students and assign multiple subjects to that particular student. This’ll be a vice-versa process.

So, this way we can define Many to Many Relationships in Laravel through migration and foreign references.

The same way we can create a common media table which will store only media file names and ‘id’ of the other tables (users, posts, products, etc.). This will be polymorphic many to many relationships and through 2 tables only.

Support On Demand!

Laravel