# Morphs vs Foreign Key

## 🆚 Comparison

| Aspect                             | Morphs                                                             | Foreign Key                                              |
| ---------------------------------- | ------------------------------------------------------------------ | -------------------------------------------------------- |
| Primary Use Case                   | When multiple models can have the same type of relationship        | When the relationship is clear and specific              |
| Table Structure                    | Single table to store relationships for multiple types             | Separate table for each relationship                     |
| Columns in the Table               | `morphable_id`, `morphable_type`                                   | `foreign_key_id`                                         |
| Flexibility                        | High, can easily add new models                                    | Less, requires creating new relationships for each model |
| Data Integrity Enforcement         | Less, no constraints at the database level                         | High, with foreign key constraints                       |
| Common Use Cases                   | Comments, tags, images                                             | User profiles, orders, order items                       |
| Performance                        | Can be more complex and may require special handling in some cases | High, due to foreign key constraints                     |
| Maintenance                        | Easy to add new models with the same relationship                  | Easy to maintain clear relationships                     |
| Examples in Laravel                | Posts, Videos having Comments                                      | User having Profile, Order having Items                  |
| Defining Relationships in Models   | `morphMany`, `morphTo`                                             | `hasOne`, `belongsTo`, `hasMany`, `belongsToMany`        |
| Creating Relationships in Database | `morphs('commentable')`                                            | `foreign('user_id')->references('id')->on('users')`      |

## 🅰️ Morphs

{% hint style="success" %}
**การใช้งาน** : ใช้ในการสร้างความสัมพันธ์แบบ polymorphic ที่โมเดลหลายตัวสามารถเชื่อมโยงกับโมเดลเดียวกันได้ ใช้สองคอลัมน์: `morphable_id` และ `morphable_type` เพื่อระบุ id และชนิดของโมเดล

**ข้อดี** : มีความยืดหยุ่นสูง เหมาะกับกรณีที่ต้องการเก็บข้อมูล เช่น Posts, Videos, Comments

**ข้อเสีย** : มีความซับซ้อน ไม่สามารถรักษาความถูกต้องของข้อมูล เนื่องจากไม่มี Foreign Key Constraints และไม่มีประสิทธิภาพในการสืบค้นข้อมูล
{% endhint %}

## 🅱️ Foreign Key

{% hint style="success" %}
**การใช้งาน** : ใช้ในการสร้างความสัมพันธ์แบบ One-to-One หรือ One-to-Many

**ข้อดี** : ไม่ซับซ้อน สามารถรักษาความถูกต้องของข้อมูลได้ดีเนื่องจากการอ้างอิงด้วย Foreign Key constraints และมีประสิทธิภาพในการสืบค้นข้อมูล

**ข้อเสีย** : ขาดความยืดหยุ่น ไม่สามารถใช้กับความสัมพันธ์ที่ต้องการเชื่อมโยงกับหลายตารางพร้อมกันได้
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://idt-nida.gitbook.io/developer-guideline/must-you-know/comparison/morphs-vs-foreign-key.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
