Laravel 7 Polymorphic Relationship Example Tutorial

Hello Artisan 

In this tutorial i am going to show polymorphic relationship laravel tutorial. In this tutorial i will show step by step many to many polymorphic relationship example from scratch.

In this article, you can understand how to create polymorphic many-to-many relationships with migration with a foreign key schema for one 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 example, i will create "posts", "videos", "tags" and "taggables" tables. each table is connected with each other. now we will create many to many polymorphic relationships with each other by using laravel Eloquent Model.

Now let's start our many to many polymorphic relationship into our posts, videos and tags table. 

polymorphic-relationship-example-laravel

Step 1  : Create Migrations:

Now we have to create our four table like "posts", "videos", "tags" and "taggables" table. so create it one after another.

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");

});

Read Also : Eloquent Relationships in Laravel

Step 2 : Create Models

Now to check our many to many polymorphic relationship we have to create Post, Video and Tag table model. we have to use"morphToMany()" and "morphedByMany()" for relationship of both model.

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 : Retrieve Records From Database

Now we can retrieve data from database using below mechanism. So just follow this below example to retrive data.

you can find post tags like

$post = Post::find(1);	
 
dd($post->tags);

you can find video tags like

$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);

Step 4 : Save Records:

We can save tag for the following post into database by the following manner

$post = Post::find(1);	
 
$tag = new Tag;
$tag->name = "Laravel";
 
$post->tags()->save($tag);

We can save tag for the following video into database by the following manner

$video = Video::find(1);	
 
$tag = new Tag;
$tag->name = "Madona";
 
$video->tags()->save($tag);

We can also save multiple tag like that

$post = Post::find(1);	
 
$tag1 = new Tag;
$tag1->name = "Laravel";
 
$tag2 = new Tag;
$tag2->name = "jQuery";
 
$post->tags()->saveMany([$tag1, $tag2]);

You can use sync or attach to save multiple tag like that

$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]);

Read also : Understanding Constructor and Method Dependency Injection in Laravel

Hope you understood this many to many polymorphic relationship example. 

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website