For getting every new tutorial link Please join our telegram group

Part 9 laravel many-to-many polymorphic relationship by morphToMany() and morphedByMany() method

This tutorial about laravel many-to-many polymorphic relationship by morphToMany() and morphedByMany() method with example and hindi video tutorial.


Also ask: laravel morphToMany() relationship tutorial, many-to-many polymorphic relationship not proper working, how to get morphToMany() relationship data and laravel morphToMany() and morphedByMany() method.

How to get laravel many-to-many polymorphic relationship by morphToMany() method

Laravel many-to-many polymorphic relationship define when more than one tables related to a other common table with many-to-many relationship. In this relationship we use morphToMany() method in model for getting common table data.


Let understand by example we have four tables posts, videos, tags and taggables. Here two main tables are posts and videos or third table tags is common table which contain both table data and last table is taggables that define many-to-many polymorphic relationship between posts and tags or videos and tags

Table structure of many-to-many polymorphic relationship

Tables
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

In above example tag_id is id of 'tags' table taggable_id contain id of 'posts' and 'videos' table and taggable_type have Model path of both models.

Model structure of many-to-many relationship by morphToMany()

Post Model

In Post model we define a morphToMany() relationship for getting tags data. morphToMany() method first argument pass name of related model and second argument is relationship name like 'taggable'.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
    public function tags(){
        return $this->morphToMany(Tag::class, 'taggable');
    }
}

In Video model get tags data by same way.

Inverse of many-to-many polymorphic relationship by morphedByMany() method

If we want to get post and videos data by Tag model than use inverse relationship. Laravel many-to-many inverse polymorphic relationship use morphedByMany() for getting data. morphedByMany() method first argument is model name and second argument is relationship name. In below code example we will get posts and videos data by tag model.

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    // tag posts data
    public function posts(){
        return $this->morphedByMany(Post::class, 'taggable');
    }

    // tag videos data
    public function videos(){
        return $this->morphedByMany(Video::class, 'taggable');
    }
}

Laravel many-to-many polymorphic relationship data return in controller

Once you define relationship in model than use in controller for getting data for example get a video all tags data in VideoController.

use App\Models\Video;

$video = Video::with('tags')->find(1);

foreach ($video->tags as $tag) {
    echo $tag;
}

Now in other example we will get Inverse relationship data like get a tag all videos and posts data.

use App\Models\Tag;

$tag = Tag::find(1);

foreach ($tag->posts as $post) {
    echo $post;
}

foreach ($tag->videos as $video) {
    echo $video;
}

You read this laravel tutorial on advanced web tutorial. here we provide laravel beginners to advanced tutorial in hindi video.

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links