Laravel 10 Many to Many Polymorphic Relationship Example

Laravel 10 Many to Many Polymorphic Relationship Example

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.

Recommended Laravel Tutorials

Recommended:-Laravel Try Catch

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *