In this tutorial you will learn about the Laravel Many to Many Polymorphic Relationship Example and its application with practical example.
In this Laravel Many to Many Polymorphic Relationship Example tutorial, I’ll show you how to implement laravel many to many polymorphic relationship in laravel eloquent model. You will also learn how to use create, and retrieve records from database tables using Many to Many Polymorphic Relationship.
Laravel Many to Many Polymorphic Relationship Example
In laravel, “morphToMany()” and “morphedByMany()” eloquent method allows you to create Many to Many Polymorphic Relationship in your laravel eloquent models.
In this step by step tutorial I’ll guide you through to create many to many polymorphic relationship and learn how to use this relationship:
Create Migration File
In this example we will create posts, videos, tags and taggables migration files as following:
database/migrations/posts
1 2 3 4 5 6 7 8 9 |
Schema::create('posts', function (Blueprint $table) { $table->bigIncrements('id'); $table->string("name"); $table->timestamps(); }); |
database/migrations/videos
1 2 3 4 5 6 7 8 9 |
Schema::create('videos', function (Blueprint $table) { $table->bigIncrements('id'); $table->string("name"); $table->timestamps(); }); |
database/migrations/tags
1 2 3 4 5 6 7 8 9 |
Schema::create('tags', function (Blueprint $table) { $table->bigIncrements('id'); $table->string("name"); $table->timestamps(); }); |
database/migrations/taggables
1 2 3 4 5 6 7 8 9 |
Schema::create('taggables', function (Blueprint $table) { $table->integer("tag_id"); $table->integer("taggable_id"); $table->string("taggable_type"); }); |
Create many to many polymorphic relationships in model
Next, create many to many polymorphic relationships as follow:
app/Post.php
1 2 3 4 5 6 7 8 9 10 11 12 |
namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function tags() { return $this->morphToMany(Tag::class, 'taggable'); } } |
app/Video.php
1 2 3 4 5 6 7 8 9 10 11 12 |
namespace App; use Illuminate\Database\Eloquent\Model; class Video extends Model { public function tags() { return $this->morphToMany(Tag::class, 'taggable'); } } |
app/Tag.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
namespace App; use Illuminate\Database\Eloquent\Model; class Tag extends Model { public function posts() { return $this->morphedByMany(Post::class, 'taggable'); } public function videos() { return $this->morphedByMany(Video::class, 'taggable'); } } |
Insert and retrieve a record using many to many polymorphic relationship
Now I will show you how to insert and retrieve a record from posts, videos tags, and taggables table in many to many polymorphic relationship:
Retrieve Records From table
Find post tags as follow:
1 2 3 |
$post = Post::find(1); dd($post->tags); |
Find video tags as follow:
1 2 3 |
$video = Video::find(1); dd($video->tags); |
and you can find post and video of a specific tag like
1 2 3 |
$tag = Tag::find(1); dd($tag->posts); |
1 2 3 |
$tag = Tag::find(1); dd($tag->videos); |
Insert Records in Table
Now we can Insert tag for the post into DB table as following:
1 2 3 4 5 6 |
$post = Post::find(1); $tag = new Tag; $tag->name = "Laravel"; $post->tags()->save($tag); |
Insert tag for the following video into DB table as follow:
1 2 3 4 5 6 |
$video = Video::find(1); $tag = new Tag; $tag->name = "Madona"; $video->tags()->save($tag); |
Insert Multiple Tags for Post:
1 2 3 4 5 6 7 8 9 |
$post = Post::find(1); $tag1 = new Tag; $tag1->name = "Laravel"; $tag2 = new Tag; $tag2->name = "jQuery"; $post->tags()->saveMany([$tag1, $tag2]); |
Using sync or attach to insert multiple tag as follow:
1 2 3 4 5 6 |
$post = Post::find(1); $tag1 = Tag::find(3); $tag2 = Tag::find(4); $post->tags()->attach([$tag1->id, $tag2->id]); |
Or use sync
1 2 3 4 5 6 |
$post = Post::find(1); $tag1 = Tag::find(3); $tag2 = Tag::find(4); $post->tags()->sync([$tag1->id, $tag2->id]); |