Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53eb726

Browse files
committed
feat: Add documentation for automatic eager loading of relationships in Eloquent
1 parent 11248b2 commit 53eb726

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function sidebarProgrammingLanguage()
5757
{text: "Vite 静态资源替换", link: "/programming-language/php/laravel/blade/vite"},
5858
{text: "pint 代码风格", link: "/programming-language/php/laravel/code-style/pint"},
5959
{text: "监听数据库执行", link: "/programming-language/php/laravel/eloquent/monitor-database-statements"},
60+
{text: "自动惰性加载关联关系", link: "/programming-language/php/laravel/eloquent/automatically-eager-load-relationships"},
6061
{text: "安装指定版本 Laravel", link: "/programming-language/php/laravel/installing-a-specific-version-of-laravel"},
6162
]
6263
},
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 自动惰性加载关联关系 {#automatically-eager-load-relationships}
2+
3+
在 Laravel 中,Eloquent ORM 提供了自动惰性加载关联关系的功能。
4+
5+
通过 `Model::automaticallyEagerLoadRelationships()` 开启,这对于减少 N+1 查询问题非常有用。
6+
7+
```php
8+
// bootstrap/app.php
9+
10+
use Illuminate\Database\Eloquent\Model; // [!code ++]
11+
12+
return Application::configure(basePath: dirname(__DIR__))
13+
// ...
14+
->withExceptions(function (Exceptions $exceptions) {
15+
//
16+
})
17+
->booting(function() { // [!code ++]
18+
Model::automaticallyEagerLoadRelationships(); // [!code ++]
19+
}) // [!code ++]
20+
->create();
21+
22+
```

0 commit comments

Comments
 (0)