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.

Read Also:  Laravel Eloquent Useful Tips and Tricks

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.

Read Also:  how to disable browser back button in php?

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 :

Read Also:  One to Many Relationship in Laravel 5.7 Eloquent

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Laravel Set Active Menu Examples
  2. Read Also:  laravel db Raw SQL Queries select insert and Update
  3. Read Also:  Vuejs Array Values Change Example with Demo
  4. Read Also:  Dropdown with Default Selected Value using Angular Example
  5. Read Also:  Bind Array List to Checkboxes using Angular Example
  6. Read Also:  Laravel 6 Update Query using Eloquent Example
  7. Read Also:  Laravel Advanced Eloquent Tips and Tricks
  8. Read Also:  Laravel Pivot Tables Tutorial Example
  9. Read Also:  jQuery Datatable Server Side Processing with codeigniter
  10. Read Also:  PHP Create directory if it doesn't exist
CLOSEX