Category : #laravel
Laravel many to many polymorphic relationship is a bit different from one to many polymorphic relationship. Many-to-many polymorphic relations are a bit more complicated than "morph one" and "morph many" relationships in laravel. For example, a Post
model and Video
model has a polymorphic relation to a Tag
model.
In this tutorial, I will show you how to create polymorphic many-to-many relationships with migration with a foreign key schema for many to many relationships, use sync with a pivot table, create records, attach records, get all records, delete, update, where condition and everything related to many to many polymorphic relationship.
In this tutorial, I will create "posts", "videos", "tags" and "taggables" tables to show you an example. Each table will be connected with each other. Now we will create many to many polymorphic relationships with each other by using laravel Eloquent Model.
Many To Many (Polymorphic) Scenario
First, let's examine the table structure required to build this many to many polymorphic relationships:
posts
id - integer
name - string
videos
id - integer
name - string
tags
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
Model Structure of Polymorphic Relationship
Now let's define the many-to-many polymorphic relationship in our model class.
app/Models/Post.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');
}
}
Now define the relationship in the Video model like:
app/Models/Video.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
/**
* Get all of the tags for the post.
*/
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
Now define the relationship in the Tag model like:
app/Models/Tag.php
<?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');
}
}
Retrieving The Relationship
Once our database table and models are defined, we may access the relationships via your model's dynamic relationship properties. For example, to access all of the tags for a post, you can use the tags
dynamic relationship property:
App\Http\Controllers\TestController.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function index($id)
{
$post = Post::find($id);
foreach ($post->tags as $tag) {
//
}
}
}
For retrieving tags for a video:
App\Http\Controllers\TestController.php
<?php
namespace App\Http\Controllers;
use App\Models\Video;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function index($id)
{
$video = Video::find($id);
foreach ($video->tags as $tag) {
//
}
}
}
You can also retrieve the parent of a polymorphic relation from the polymorphic child model that performs the call to morphedByMany
. In this case, that is the posts
or videos
methods on the Tag
model:
<?php
use App\Models\Tag;
$tag = Tag::find(1);
foreach ($tag->posts as $post) {
//
}
foreach ($tag->videos as $video) {
//
}
Recommended: Laravel One To One Polymorphic Relationship Example
Save Polymorphic Relationship Data
If we want to save or create polymorphic relationship data then we can follow the below structure:
App\Http\Controllers\TestController.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function index($id)
{
$video = Video::find(1);
$tag = new Tag;
$tag->name = "laravelia.com";
$video->tags()->save($tag);
//save tag for post
$post = Post::find(1);
$tag1 = new Tag;
$tag1->name = "laravelia.com";
$tag2 = new Tag;
$tag2->name = "laravelia.com 2";
$post->tags()->saveMany([$tag1, $tag2]);
}
}
Recommeneded: Laravel 9 One To Many Polymorphic Relationship Example
Conclusion
I have tried to discuss the clear concept of many to many polymorphic relationships and their possible use cases in laravel applications. We should also remember that many to many polymorphic relationships are not a perfect solution to everything and should only be used when convenient or feels like the right way to go.