Laravel 10 many to many polymorphic relationship example; In this tutorial, you will learn about laravel many to many polymorphic relationship and how to use create, and retrieve records from database tables using this relationship.
Using “morphToMany()” and “morphedByMany()” eloquent method, you can create Many to Many Polymorphic Relationship in your laravel eloquent models.
This tutorial show
Laravel Many to Many Polymorphic Relationship Example
Follow the following steps to create many to many polymorphic relationship and learn how to use this relationship:
Step 1: Create Migration File
First of all, create posts, videos, tags and taggables migration files with following files:
database/migrations/posts
Schema::create('posts', function (Blueprint $table) { $table->bigIncrements('id'); $table->string("name"); $table->timestamps(); });
database/migrations/videos
Schema::create('videos', function (Blueprint $table) { $table->bigIncrements('id'); $table->string("name"); $table->timestamps(); });
database/migrations/tags
Schema::create('tags', function (Blueprint $table) { $table->bigIncrements('id'); $table->string("name"); $table->timestamps(); });
database/migrations/taggables
Schema::create('taggables', function (Blueprint $table) { $table->integer("tag_id"); $table->integer("taggable_id"); $table->string("taggable_type"); });
Step 2: Create many to many polymorphic relationships in model
Next, create many to many polymorphic relationships as follow:
app/Post.php
namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function tags() { return $this->morphToMany(Tag::class, 'taggable'); } }
app/Video.php
namespace App; use Illuminate\Database\Eloquent\Model; class Video extends Model { public function tags() { return $this->morphToMany(Tag::class, 'taggable'); } }
app/Tag.php
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'); } }
Step 3: Insert and retrieve a record using many to many polymorphic relationship
Now you will learn how to insert and retrieve a record from posts, videos tags, and taggables table using many to many polymorphic relationship:
Retrieve Records From table
Find post tags as follow:
$post = Post::find(1); dd($post->tags);
Find video tags as follow:
$video = Video::find(1); dd($video->tags);
and you can find post and video of a specific tag like
$tag = Tag::find(1); dd($tag->posts);
$tag = Tag::find(1); dd($tag->videos);
Insert Records in Table
Insert tag for the following post into DB table as follow:
$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:
$video = Video::find(1); $tag = new Tag; $tag->name = "Madona"; $video->tags()->save($tag);
Insert Multiple Tags for Post:
$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:
$post = Post::find(1); $tag1 = Tag::find(3); $tag2 = Tag::find(4); $post->tags()->attach([$tag1->id, $tag2->id]);
Or use sync
$post = Post::find(1); $tag1 = Tag::find(3); $tag2 = Tag::find(4); $post->tags()->sync([$tag1->id, $tag2->id]);
Conclusion
In this tutorial, you have learned how to create and use many to many polymorphic relationship in laravel apps.