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.
/** * 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'); }
/** * 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'); }
<?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'); } }
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(); }
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.