In this article, we will explore Laravel 8/9/10 many-to-many polymorphic relationships. These relationships are more complex than morph one and morph many relationships and can be utilized in Laravel 6, Laravel 7, Laravel 8 and Laravel 10. To access them, we will use the morphToMany() and morphedByMany() functions.
In this example, we will create migrations for posts, taggables, and videos, along with models for Post, Taggable, and Video. We will cover creating records, retrieving records, and attaching records.
Let's dive in!
Now we will create migration for posts, tags, videos, and taggable tables.
Posts Table:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Tags Table:
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Videos Table:
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Taggables Table:
Schema::create('taggables', function (Blueprint $table) {
$table->integer("tag_id");
$table->integer("taggable_id");
$table->string("taggable_type");
});
Now, we will create Post, Tag, and Video model. we will also use morphToMany() and morphedByMany() for relationship of both model.
Post Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Get all of the tags for the post.
*/
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
Video Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
/**
* Get all of the tags for the video.
*/
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
Tag Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
/**
* Get all of the posts that are assigned this tag.
*/
public function posts()
{
return $this->morphedByMany(Post::class, 'taggable');
}
/**
* Get all of the videos that are assigned this tag.
*/
public function videos()
{
return $this->morphedByMany(Video::class, 'taggable');
}
}
Once your database table and models are defined, you may access the relationships via your models. For example, to access all of the tags for a post, you may use the tags
dynamic relationship property.
$post = Post::find(1);
foreach ($post->tags as $tag) {
//
}
You may retrieve the parent of a polymorphic relation from the polymorphic child model by accessing the name of the method.
$tag = Tag::find(1);
foreach ($tag->posts as $post) {
//
}
foreach ($tag->videos as $video) {
//
}
Now, we will create records in the tag table.
$post = Post::find(1);
$tag = new Tag;
$tag->name = "Techsolutionstuff";
$post->tags()->save($tag);
$post = Post::find(1);
$tag1 = Tag::find(1);
$tag2 = Tag::find(2);
$post->tags()->attach([$tag1->id, $tag2->id]);
$post = Post::find(1);
$tag1 = Tag::find(1);
$tag2 = Tag::find(2);
$post->tags()->sync([$tag1->id, $tag2->id]);
To sum it up, we've delved into the world of many-to-many polymorphic relationships in Laravel 8, Laravel 9, Laravel 10. These relationships might sound complex, but once you understand them, they can be quite powerful.
We've learned how to create migrations, models, and work with records in this context. With this knowledge, you can now use many-to-many polymorphic relationships effectively in your Laravel projects, whether you're working in version 6, 7, or 8.
Happy coding!
You might also like: