For getting every new tutorial link Please join our telegram group

Part 8 laravel one to many polymorphic relationship by morphMany() with hindi video

In this laravel tutorial we will discuss eloquent model one-to-many polymorphic relationship by morphMany() method tutorial with hindi video.


Also ask laravel morphMany() relationship tutorial, one-to-many polymorphic relationship not working, how to get data by morphMany() relationship.

Laravel one-to-many polymorphic relationship

Polymorphic relationship define when one child Model connect to many other Models. one-to-many polymorphic relationship work same as one-to-many relationship, here child model connect to more than one model with one to many relationship.

Let understand by example we have two models Topic and Subtopic now we make some tutorials for both. Topic and Subtopic have multiple Tutorial data so we make here morphMany() relationship for getting both model data.

Table structure of one-to-many polymorphic relationship

Tables
topics
    id - integer
    name - string

subtopics
    id - integer
    name - string

tutorials
    id - integer
    url - string
    tutorialable_id - integer
    tutorialable_type - string

In above example tutorialable_id contain id of topics and subtopics table and tutorialable_type have Model path of both models.

Model structure of one-to-many relationship

Tutorial Model

In Tutorial model we define a tutorialable() method with morphTo() relationship which is get topic and subtopic data which is related to Tutorial model.

class Tutorial extends Model
{
    public function tutorialable() {
        return $this->morphTo();
    }
}
Topic Model


class Topic extends Model
{
    public function tutorials() {
        return $this->morphMany(Tutorial::class, 'tutorialable');
    }
}

In Topic model a method tutorials which contain morphMany relationship. morphMany() method contain first argument child model data and second argument child model polymorphic method name.

Subtopic Model
class Subtopic extends Model
{
    public function tutorials() {
        return $this->morphMany(Tutorial::class, 'tutorialable');
    }
}

In subtopic model we have use tutorials method by same like Topic model

One-to-many relationship data return in controller

once database table or model method define than use relationship in controller throw model. In controller we get all tutorials that related to Topic model.

use App\Models\Topic;

$topic = Topic::with('tutorials')->find(1);

foreach ($topic->tutorials as $tutorial) {
    //
}

Inverse One-to-many polymorphic relationship

In example if we want to get parent topic or subtopic by Tutorial model than use tutorialable() method for getting both tables data. Inverse relationship always give single row data.

use App\Models\Tutorial;

$tutorial = Tutorial::find(1);

$tutorialable = $tutorial->tutorialable;

Here if customize column names tutorialable_id and tutorialable_type than these pass in morphMany() method second and third argument and first argument contain __function__.

public function tutorialable(){
    return $this->morphTo(__FUNCTION__, 'tutorialable_type', 'tutorialable_id');
}

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