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.4.0(4mo ago)62156.8k↓40.7%104MITPHPPHP ^8.1CI failing

Since Oct 10Pushed 3mo 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 today

READMEChangelogDependencies (8)Versions (11)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

60

—

FairBetter than 98% of packages

Maintenance79

Regular maintenance activity

Popularity46

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity77

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 ~299 days

Recently: every ~360 days

Total

10

Last Release

129d 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

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M95](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[spatie/laravel-sluggable

Generate slugs when saving Eloquent models

1.6k12.9M309](/packages/spatie-laravel-sluggable)[watson/validating

Eloquent model validating trait.

9803.5M53](/packages/watson-validating)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[cybercog/laravel-love

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

1.2k332.0k1](/packages/cybercog-laravel-love)

PHPackages © 2026

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