loading...

Part 3: Testing Model Relationships in Laravel — POLYMORPHIC

tonyfrenzy profile image tony Originally published at Medium on ・3 min read

Part 3: Testing Model Relationships in Laravel — POLYMORPHIC

Source: Better Through Code

This part deals with more complex model relationships, the polymorphic type. Just like before, we shall stick to the official documentation examples and work out befitting tests. To have a low down on the earlier parts see link directly below 👇

POLYMORPHIC RELATIONSHIPS

A polymorphic relationship allows a target model to belong to more than one type of model using a single association.  — Laravel.com

1. One-to-One Polymorphic Relationship

A one-to-one polymorphic relation is similar to a simple one-to-one relation; however, the target model can belong to more than one type of model on a single association.

Scenario:

A blog Post and a User may share a polymorphic relation to an Image model. Using a one-to-one polymorphic relation allows you to have a single list of unique images that are used for both blog post s and user accounts

morphTo()

— An image table can be morph ed in to any model (i.e serve many different models), eg User or Post model in our case.

// App/Image.php
...
public function imageable() 
{ 
 return $this->morphTo(); 
}

morphOne()

  • — A user/post model can morph one instance of the same image model table (i.e make use of same image table but one record per user/post).
  • — INVERSE of morphTo relationship.
// App/User.php
...
public function image() 
{ 
 return $this->morphOne(Image::class, 'imageable'); 
}

Files relevant to this test 👇

- **MODEL FILES(App/)
 -- User.php 
 -- Post.php
 -- Image.php

- **MIGRATION FILES(database/migrations/)
 -- 2014\10\12\000000\create\users\table.php
 -- 2019\09\27\100604\create\posts\table.php
 -- 2019\09\28\143143\create\images\table.php

- **MODEL FACTORY FILES(database/factories/)
 -- UserFactory.php 
 -- PostFactory.php
 -- ImageFactory.php

- **UNIT TEST FILES(tests/Units/)
 -- UserTest.php 
 -- PostsTest.php
 -- ImagesTest.php

2. One-to-Many (Polymorphic)

A “one-to-many polymorphic” relation is similar to a simple one-to-many relation; however, the target model can belong to more than one type of model on a single association. — Laravel.com

Scenario:

Users of your application can “comment” on both posts and videos. Using polymorphic relationships, you may use a single comments table for both of these scenarios.

morphTo()

— A comment table can be morph ed to any model (i.e serve many different models), eg Video or Post model in our case.

// App/Comment.php
...
public function commentable() 
{ 
 return $this->morphTo(); 
}

morphMany()

  • — A video/post model can morph many instances of the same comment model table (i.e make use of same comment table for many records).
  • — INVERSE of morphTo relationship.
// App/Video.php
...
public function comments() 
{ 
 return $this->morphMany(Comment::class, 'commentable'); 
}

Files relevant to this test 👇

- **MODEL FILES(App/)
 -- User.php 
 -- Post.php
 -- Video.php
 -- Comment.php

- **MIGRATION FILES(database/migrations/)
 -- 2019\09\28\211240\create\videos\table.php
 -- 2019\09\28\221627\add\commentable\id\and\commentable\type\to\comments\table.php

- **MODEL FACTORY FILES(database/factories/)
 -- VideoFactory.php

- **UNIT TEST FILES(tests/Unit/)
 -- PostsTest.php
 -- VideosTest.php
 -- CommentsTest.php

3. Many-to-Many (Polymorphic)

In progress…

Kindly check back soon

Posted on by:

tonyfrenzy profile

tony

@tonyfrenzy

Web Software Developer. NodeJs | Laravel | VueJs | SEO.

Discussion

markdown guide