Laravel 9 Many To Many Polymorphic Relationship

Websolutionstuff | Apr-06-2022 | Categories : Laravel PHP

In this article, we will see laravel 9 many to many polymorphic relationship. many to many polymorphic relationship more complicated compare to morph one and morph many relationship.

many to many polymorphic relationship can use in laravel 6, laravel 7 and laravel 8 and for access many to many polymorphic relationship use morphToMany() and morphedByMany().

So, let's see laravel 9 polymorphic, many to many polymorphic laravel 9, polymorphic relationship laravel 9 example, laravel 9 relationships, morphToMany, morphedByMany in laravel 9

In this example, we will create migration for post, taggables and videos and also create model Post, Taggable and Video. Also see create records, retrieve records and attach records.

laravel_9_many_to_many_polymorphic_relationship

 

 

Create Migration :

Now we will create migration for posts, tags, videos and taggable table.

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

});

 

Create Model :

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');
    }
}

 

Retrieve Records :

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) {
    //
}

 

 

Create Records :

Now, we will create a record in the post table. 

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

In this example, we will attach a tag to the post.

$post = Post::find(1);	
 
$tag1 = Tag::find(1);
$tag2 = Tag::find(2);
 
$post->tags()->attach([$tag1->id, $tag2->id]);

In this example, we will sync a tag with the post.

$post = Post::find(1);	
 
$tag1 = Tag::find(1);
$tag2 = Tag::find(2);
 
$post->tags()->sync([$tag1->id, $tag2->id]);

 


You might also like :

Recommended Post
Featured Post
Razorpay Payment Gateway Integration in Laravel 10 and VueJS 3
Razorpay Payment Gateway Integ...

In the rapidly advancing world of technology, the combination of Laravel 10 and VueJS 3 is unparalleled and exceptionall...

Read More

Aug-30-2023

How to Use ngStyle in Angular for Dynamic Styling?
How to Use ngStyle in Angular...

A user-friendly and dynamic interface is paramount to engaging users on your site. Angular is one of the most powerful a...

Read More

Oct-04-2023

How to File Upload in Angular 17 Tutorial
How to File Upload in Angular...

In this article, we'll see how to file upload in the angular 17 tutorial. Here, we'll learn about file uplo...

Read More

Apr-03-2024

Laravel 8 Artesaos SEOTools Tutorial
Laravel 8 Artesaos SEOTools Tu...

Hello Devs, In this tutorial we will learn how to use SEOTools in laravel 8. we will give you example of se...

Read More

Jun-07-2021