我已经在这里阅读了文档:https : //github.com/laravel/framework/pull/25997
我想知道的是,通过使用withCount()我们,我们只是对记录进行加载计数,而不是获取所有关系数据。
那么通过使用loadCount()我们可以做什么?
请用简单的文字简单说明。谢谢
Laravel 5.7.10版本引入了loadCount雄辩的收集方法。根据laravel新闻。
loadCount是在Eloquent集合上加载关系计数的功能。在使用此功能之前,您只能加载关系,但是现在您可以调用loadCount()来获取所有关系的计数。
拉取请求通过以下示例说明了如何使用loadCount():
$events = Event::latest()->with('eventable')->paginate();
$groups = $events->map(function ($event) {
return $event->eventable;
})->groupBy(function ($eventable) {
return get_class($eventable);
});
$groups[Post::class]->loadCount('comments');
$groups[Comment::class]->loadCount('hearts');
return new EventIndexResponse($events);
Run Code Online (Sandbox Code Playgroud)