<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/aggregate-queries-without-n-1-withcount-withsum-withavg-and-withexists-qbv8qfwfz" -->

---
title: Aggregate Queries Without N+1: withCount, withSum,...
description: Dashboards showing aggregated data like order counts and totals often trigger N+1 query problems in Laravel apps because the pattern looks harmless in code....
canonical: https://daily.dev/posts/aggregate-queries-without-n-1-withcount-withsum-withavg-and-withexists-qbv8qfwfz
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Aggregate Queries Without N+1: withCount, withSum, withAvg, and withExists | daily.dev
og:description: Dashboards showing aggregated data like order counts and totals often trigger N+1 query problems in Laravel apps because the pattern looks harmless in code....
og:url: https://daily.dev/posts/aggregate-queries-without-n-1-withcount-withsum-withavg-and-withexists-qbv8qfwfz
og:image: https://api.daily.dev/og/posts/QBv8Qfwfz.png
og:image:alt: Aggregate Queries Without N+1: withCount, withSum, withAvg, and withExists
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Aggregate Queries Without N+1: withCount, withSum, withAvg, and withExists

**[PHP Architect](https://daily.dev/sources/phparchitect)** · [@shocm](https://daily.dev/shocm) · 1 min read · 0 upvotes · 0 comments

## Summary

Dashboards showing aggregated data like order counts and totals often trigger N+1 query problems in Laravel apps because the pattern looks harmless in code. Eloquent provides withCount(), withSum(), withAvg(), and withExists() to fold these aggregate lookups into the original query instead of firing a separate query per row.

## Content

A dashboard listing orders with their item count and total is one of the most common places an N+1 sneaks into a Laravel app, precisely because it looks harmless: the code looks harmless but that's 20 orders plus 40 additional queries. Eloquent has purpose-built helpers that fold every one of those into the original query — withCount(), withSum(), withAvg(), and withExists().

https://laravelmagazine.com/aggregate-queries-without-n-plus-1-withcount-withsum-withavg-and-withexists

## Similar posts on daily.dev

- [Eager Loading and Aggregate Functions](https://daily.dev/posts/eager-loading-and-aggregate-functions-d2gqfnuck) · Larablog · 0 upvotes · 0 comments
- [Smarter Eager Loading](https://daily.dev/posts/smarter-eager-loading-xr3epwcr3) · Larablog · 0 upvotes · 0 comments
- [Eloquent Performance and Database Design: Evidence Before Eager Loading](https://daily.dev/posts/eloquent-performance-and-database-design-evidence-before-eager-loading-j4czrk9ss) · W endell Adriel · 45 upvotes · 7 comments
- [Is Eloquent Actually Slow?](https://daily.dev/posts/is-eloquent-actually-slow--in5zcay8f) · Larablog · 4 upvotes · 0 comments

---

Tags: [#php](https://daily.dev/tags/php), [#laravel](https://daily.dev/tags/laravel)

[View this post on daily.dev](https://daily.dev/posts/aggregate-queries-without-n-1-withcount-withsum-withavg-and-withexists-qbv8qfwfz)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/aggregate-queries-without-n-1-withcount-withsum-withavg-and-withexists-qbv8qfwfz","headline":"Aggregate Queries Without N+1: withCount, withSum, withAvg, and withExists","text":"Dashboards showing aggregated data like order counts and totals often trigger N+1 query problems in Laravel apps because the pattern looks harmless in code. Eloquent provides withCount(), withSum(), withAvg(), and withExists() to fold these aggregate lookups into the original query instead of firing a separate query per row.","url":"https://daily.dev/posts/aggregate-queries-without-n-1-withcount-withsum-withavg-and-withexists-qbv8qfwfz","datePublished":"2026-08-21T18:09:55.034Z","dateModified":"2026-08-21T18:10:13.795Z","author":{"@type":"Person","name":"Eric Johnson","url":"https://daily.dev/shocm","image":"https://media.daily.dev/image/upload/s--EPMJ-a-K--/f_auto/v1755890250/avatars/avatar_y7ct3dSEp45NFSALYHve6?_a=BAMClqZW0","description":"PHP Architect team member. I am powered by 16-year-old single malt Scotch and baseball.","worksFor":{"@type":"Organization","name":"php[architect]","logo":"https://res.cloudinary.com/daily-now/image/upload/s--10MwB_VH--/f_auto/v1726147436/companies/phparch"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":400}},"image":"https://media.daily.dev/image/upload/s--zQRqZBoo--/f_auto/v1787335795/posts/QBv8Qfwfz?_a=BAMAMicg0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/phparchitect","name":"PHP Architect"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"PHP Architect","item":"https://daily.dev/squads/phparchitect"},{"@type":"ListItem","position":3,"name":"Aggregate Queries Without N+1: withCount, withSum, withAvg, and withExists"}]}
```

