Part 3: Testing Model Relationships in Laravel — POLYMORPHIC
Tags: eloquentorm, laravel, codequality, phpunit
Published: 2019-10-08 11:53:16

---
title: Part 3: Testing Model Relationships in Laravel — POLYMORPHIC
published: true
tags: eloquent-orm,laravel,code-quality,phpunit
canonical_url: 
---

### Part 3: Testing Model Relationships in Laravel — POLYMORPHIC

![](https://cdn-images-1.medium.com/max/1024/1*6NdGWcFOM1jbb--dAcZnQQ.png)<figcaption>Source: <a href="https://medium.com/better-through-code?source=post_page-----2894f43217e4----------------------">Better Through Code</a></figcaption>

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 👇

- Part 1: [**Intro and Schema Tests**](https://dev.to/tonyfrenzy/part-1-testing-model-relationships-in-laravel-schema-tests-37nj)
- Part 2: [**Testing Basic Model Relationships**](https://dev.to/tonyfrenzy/part-2-testing-model-relationships-in-laravel-basic-3a44)
- Part 3: **Testing Polymorphic Relationships** ⏯

### POLYMORPHIC RELATIONSHIPS

**A polymorphic relationship allows a target model to belong to more than one type of model using a single association.**  — [Laravel.com](https://laravel.com/docs/6.x/eloquent-relationships#polymorphic-relationships)

### 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(); 
}
```

{% gist https://gist.github.com/tonyfrenzy/b3f7788dae99c216867252d70a84d3cc %}

#### 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'); 
}
```

{% gist https://gist.github.com/tonyfrenzy/6fbeddec373a0491ff592ced01a0d723 %}

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](https://laravel.com/docs/6.x/eloquent-relationships#one-to-many-polymorphic-relations)

#### 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(); 
}
```

{% gist https://gist.github.com/tonyfrenzy/e8d693123cd0184d288815b37be93a4e %}

#### 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'); 
}
```

{% gist https://gist.github.com/tonyfrenzy/e98b70a706db44d24c85d7eedc6b66ab %}

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

- 👉Part 1: [**Intro and Schema Tests**](https://dev.to/tonyfrenzy/part-1-testing-model-relationships-in-laravel-schema-tests-37nj)
- 👉Part 2: [**Testing Basic Model Relationships**](https://dev.to/tonyfrenzy/part-2-testing-model-relationships-in-laravel-basic-3a44)
- ⏯Part 3: **Testing Polymorphic Relationships**