# Lazy Loading vs Eager Loading

## 🆚 Comparison

| Feature        | Lazy Loading                                      | Eager Loading                                                           |
| -------------- | ------------------------------------------------- | ----------------------------------------------------------------------- |
| Definition     | Loads related objects only when accessed          | Loads related objects immediately                                       |
| Performance    | Can reduce initial load time                      | May increase initial load time                                          |
| Query Control  | More granular control over fetched data           | Less control over fetched data                                          |
| Complexity     | Simpler to implement initially                    | More complex to implement correctly                                     |
| Network Calls  | May require multiple queries                      | Usually requires fewer queries                                          |
| Usage Scenario | Suitable for large datasets with selective access | Suitable for small datasets or when all related data is needed together |

## 🅰️ Lazy Loading

{% hint style="success" %}
**การใช้งาน** : ใช้เมื่อต้องการประหยัดทรัพยากรและเพิ่มประสิทธิภาพในการโหลดข้อมูล เนื่องจากข้อมูลจะถูกโหลดเฉพาะเมื่อมีความจำเป็นจริง ๆ

**ข้อดี** : ช่วยลดการใช้ทรัพยากรในขณะที่ไม่จำเป็น และ ช่วยเพิ่มประสิทธิภาพของแอปพลิเคชันโดยลดเวลาการโหลดข้อมูลที่ไม่ได้ใช้

**ข้อเสีย** : อาจทำให้เกิดปัญหาการเรียกข้อมูลซ้ำซ้อนได้ หรือ อาจทำให้เกิดปัญหาการแก้ไขข้อมูลที่มีการอ้างอิงระหว่างข้อมูล
{% endhint %}

```php
$project = Project::find(1);
$user = $project->user;
```

## 🅱️ Eager Loading

{% hint style="success" %}
**การใช้งาน** : ใช้เมื่อต้องการความแม่นยำในการดึงข้อมูลแบบเต็มรูปแบบตั้งแต่ต้น ซึ่งอาจช่วยลดเวลาในการเข้าถึงข้อมูลที่ซับซ้อน

**ข้อดี** : ช่วยให้การเข้าถึงข้อมูลที่ซับซ้อนเร็วขึ้น และ ลดความซับซ้อนในการจัดการข้อมูลที่มีความสัมพันธ์มาก

**ข้อเสีย** : ใช้ทรัพยากรมากขึ้นในการโหลดข้อมูลเป็นจำนวนมาก หรือ อาจทำให้ล่าช้าในการโหลดหน้าเว็บหรือการทำงานที่ต้องการข้อมูลบางส่วนเท่านั้น
{% endhint %}

```php
$projects = Project::with('user')->get();

foreach ($projects as $project) {
    $user = $project->user;
}
```


---

# 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/lazy-loading-vs-eager-loading.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.
