Polymorphic One to Many Relationship Example

Today, We want to share with you Polymorphic One to Many Relationship Example.In this post we will show you , hear for Laravel 5.7 One to Many Polymorphic Relationship we will give you demo and example for implement.In this post, we will learn about One-To-Many polymorphic relationship with an example.

Polymorphic One to Many Relationship Example

There are the Following The simple About Polymorphic One to Many Relationship Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Eloquent Polymorphic One to Many Relationships, so the Laravel-Many-to-one Polymorphic relationship for this example is following below.

Laravel Eloquent Relationships Tutorial with Examples

Example details:

In this Example we have 3 models (Boy, Girl and Mobile), and 3 tables (boy, girl and mobiles).

Business Rules:

  • The Boy (customer) can buy many Mobiles.
  • The Girl (customer) can buy many Mobiles.
  • The Mobile can be bought by one customer (Boy or Girl).

Relationship Details:

The Mobile table should store the Customer ID and the Customer Type.
“customer” is a name given to a group of models (Boy and Girl). And it’s not limited to two. The customer type is the real name of the model.

Laravel 5.7 Eloquent Models:

class Boy
{
    public function mobiles()
    {
        return $this->morphMany(Mobile::class, 'customer');
    }
}
class Girl
{
    public function mobiles()
    {
        return $this->morphMany(Mobile::class, 'customer');
    }
}
class Mobile
{
    public function customer()
    {
        return $this->morphTo();
    }
}

Laravel Define Database Migrations:

Schema::create('boy', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
});
Schema::create('girl', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
});
Schema::create('mobiles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->integer('customer_id')->unsigned()->index()->nullable();
    $table->string('customer_type')->nullable();   
    // or use $table->morphs(‘customer’); instead of "customer_id" and "customer_type"
});

Store/Save Database Records:

// Create relation between customer (Boy/Girl) and Mobile.
$boy->mobiles()->saveMany([
   $mobile1, 
   $mobile2,
]);
$girl->mobiles()->saveMany([
   $mobile1, 
   $mobile2,
]);
// Or use the save() function for single model.
$boy->mobiles()->save($mobile);
$girl->mobiles()->save($mobile);

// Create relation between Mobile and customer (Boy/Girl).
$mobile1->customer()->associate($boy)->save();
$mobile2->customer()->associate($girl)->save();

Retrieve/fetch Records:

// Get customer (Boy/Girl) Mobiles
$boy->mobiles
$girl->mobiles

// Get Mobile customer (Boy and Girl)
$mobile->customer

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Polymorphic One to Many Relationship Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment