PHPackages                             muratgorken/laravel-with-where-has-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. muratgorken/laravel-with-where-has-aggregate

ActiveService-provider[Database &amp; ORM](/categories/database)

muratgorken/laravel-with-where-has-aggregate
============================================

A package that ensures compatibility between the 'withWhereHas' method and the 'withAggregate' methods

09[1 PRs](https://github.com/muratgorken/laravel-with-where-has-aggregate/pulls)PHP

Since Feb 28Pushed 2y ago1 watchersCompare

[ Source](https://github.com/muratgorken/laravel-with-where-has-aggregate)[ Packagist](https://packagist.org/packages/muratgorken/laravel-with-where-has-aggregate)[ RSS](/packages/muratgorken-laravel-with-where-has-aggregate/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

"A package that ensures compatibility between the 'withWhereHas' method and the 'withAggregate' methods."
=========================================================================================================

[](#a-package-that-ensures-compatibility-between-the-withwherehas-method-and-the-withaggregate-methods)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7bcae3da6525b2aa3387872ac39be9eff80a86c4cf86e8b84da233f0d24254e0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d75726174676f726b656e2f6c61726176656c2d776974682d77686572652d6861732d6167677265676174652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/muratgorken/laravel-with-where-has-aggregate)[![GitHub Tests Action Status](https://camo.githubusercontent.com/47ed56f5afe99a792ea2517c3a3bd7df51e052a749b85240502ead5630d7f888/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d75726174676f726b656e2f6c61726176656c2d776974682d77686572652d6861732d6167677265676174652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/muratgorken/laravel-with-where-has-aggregate/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/14ced846903177e76d0665014a607738a1b9a640f21b797afebb01a374fa3d35/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d75726174676f726b656e2f6c61726176656c2d776974682d77686572652d6861732d6167677265676174652f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/muratgorken/laravel-with-where-has-aggregate/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/d74a24173cecae1245bedafd573f7b0f2e3df51599d015c976c1ce119d64fc12/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d75726174676f726b656e2f6c61726176656c2d776974682d77686572652d6861732d6167677265676174652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/muratgorken/laravel-with-where-has-aggregate)

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

[](#installation)

You can install the package via composer:

```
composer require muratgorken/laravel-with-where-has-aggregate
```

Usage
-----

[](#usage)

For example, from an author model, we want to rank authors who have books in the drama category and authors based on the average page count of those books. And we want to do this bootstrapped. So;

```
$authorsWithDramaBooks = Author::withWhereHasAggregate('books', function ($query) {
			$query->where('type', 'drama');
		}, 'avg(page_count as page_avg)')->get();
```

in the form of $query-&gt;where.

This package also supports multiple operations.

```
$authorsWithRomanticBooks = Author::withWhereHasAggregate('books', function ($query) {
			$query->where('type', 'romantic');
		}, 'avg(page_count as page_avg', 'max(page_count) as page_max', 'min(page_count) as page_min')->get()->toArray();
```

output of the above query;

```
array:1 [▼
  0 => array:6 [▼
    "id" => 1
    "name" => "William Shakespeare"
    "page_avg" => "400.0000"
    "page_max" => 500
    "page_min" => 300
    "books" => array:2 [▼
      0 => array:5 [▼
        "id" => 1
        "name" => "Romeo and Juliet"
        "author_id" => 1
        "type" => "romantic"
        "page_count" => 500
      ]
      1 => array:5 [▼
        "id" => 2
        "name" => "Measure for Measure"
        "author_id" => 1
        "type" => "romantic"
        "page_count" => 300
      ]
    ]
  ]
]
```

this way you can run multiple aggregate functions withWhereHas.

When used withWhereHas withAggregate methods in Laravel itself, it does not run the relevant conditions in withAggregate methods even though it is the same relation. This means that if you; similar to the example above

```
$books = Author::whereHas('books', function ($query) {
    $query->where('type', 'drama');
})->withAvg('books as page_count_avg', 'page_count')->get();
```

it will calculate the average page count of all books, not just books of the drama type.

feel free to ask if you have any questions.

```
$books = Author::whereHas('books', function ($query) {
    $query->where('type', 'drama');
})->withAggregate('books as page_count_avg', 'page_count', 'avg', function($query) {
    $query->where('type', 'drama');
})->get();
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Murat Görken](https://github.com/muratgorken)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity21

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/243f7c20a1a469cfceff742851de17eaf0e093f2b17b125142fa26ae8cfefac3?d=identicon)[muratgorken](/maintainers/muratgorken)

---

Top Contributors

[![muratgorken](https://avatars.githubusercontent.com/u/8851489?v=4)](https://github.com/muratgorken "muratgorken (11 commits)")

### Embed Badge

![Health badge](/badges/muratgorken-laravel-with-where-has-aggregate/health.svg)

```
[![Health](https://phpackages.com/badges/muratgorken-laravel-with-where-has-aggregate/health.svg)](https://phpackages.com/packages/muratgorken-laravel-with-where-has-aggregate)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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