PHPackages                             watson/aggregate - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. watson/aggregate

ActiveLibrary[Database &amp; ORM](/categories/database)

watson/aggregate
================

Extend Laravel's query builder with additional relation aggregates.

5.3.0(1y ago)62145.5k↑26.8%104MITPHPPHP ^8.1

Since Oct 10Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/dwightwatson/aggregate)[ Packagist](https://packagist.org/packages/watson/aggregate)[ Docs](https://github.com/dwightwatson/aggregate)[ RSS](/packages/watson-aggregate/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (10)Used By (4)

Aggregate
=========

[](#aggregate)

Laravel Eloquent allows you to query the count of a relationship using `withCount`. Aggregate extends Eloquent by adding `withSum`, `withAvg`, `withMin` and `withMax`.

This is based off the work in [`laravel/framework#25319`](https://github.com/laravel/framework/pull/25319) - thanks to Mohammad Sharif Ahrari ([@spyp](https://github.com/spyp)).

Installation
------------

[](#installation)

You can install the package via Composer:

```
composer require watson/aggregate
```

Usage
-----

[](#usage)

The additional methods will be added by Laravel's autodiscovery feature. You can then use them the same way you already use `withCount`. [See the Laravel documentation for more on how this works](https://laravel.com/docs/5.7/eloquent-relationships#counting-related-models).

```
$orders = Order::withSum('products', 'quantity')->get();

$orders->each(function ($order) {
    $order->products_sum;
});
```

You can also select multiple aggregates in a single query, as well as alias them.

```
$orders = Order::withCount('products')->withSum('products as products_price', 'price')->get();

$orders->each(function ($order) {
    $order->products_count;

    $order->products_price;
});
```

```
$orders = Order::withCount('products')->withMax('products', 'price')->get();

$orders->each(function ($order) {
    $order->products_count;

    $order->products_max;
});
```

### Testing

[](#testing)

```
vendor/bin/phpunit
```

###  Health Score

56

—

FairBetter than 98% of packages

Maintenance66

Regular maintenance activity

Popularity47

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 78.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~290 days

Recently: every ~380 days

Total

9

Last Release

456d ago

Major Versions

1.0.0 → 2.0.02019-10-04

2.0.0 → 3.0.02020-03-10

3.0.0 → 4.0.02020-09-11

4.1.0 → 5.0.02022-03-15

PHP version history (7 changes)1.0.0PHP ^7.1

2.0.0PHP ^7.2

3.0.0PHP ^7.2.5

4.0.0PHP ^7.3

4.1.0PHP ^7.3|^8.0

5.0.0PHP ^8.0

5.1.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1100408?v=4)[Dwight Watson](/maintainers/dwightwatson)[@dwightwatson](https://github.com/dwightwatson)

---

Top Contributors

[![dwightwatson](https://avatars.githubusercontent.com/u/1100408?v=4)](https://github.com/dwightwatson "dwightwatson (18 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (4 commits)")[![spyp](https://avatars.githubusercontent.com/u/15471788?v=4)](https://github.com/spyp "spyp (1 commits)")

---

Tags

eloquentlaravelphplaraveleloquentaggregations

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/watson-aggregate/health.svg)

```
[![Health](https://phpackages.com/badges/watson-aggregate/health.svg)](https://phpackages.com/packages/watson-aggregate)
```

###  Alternatives

[silber/bouncer

Eloquent roles and abilities.

3.6k4.4M25](/packages/silber-bouncer)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
