PHPackages                             abordage/eloquent-percentile - 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. abordage/eloquent-percentile

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

abordage/eloquent-percentile
============================

Laravel Eloquent withMedian(), withPercentile(), median() and percentile() aggregate functions

2.0.0(4mo ago)63.1k[1 PRs](https://github.com/abordage/eloquent-percentile/pulls)MITPHPPHP ^8.2CI passing

Since Feb 12Pushed 1mo ago1 watchersCompare

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

READMEChangelog (7)Dependencies (6)Versions (9)Used By (0)

Laravel Eloquent withMedian, withPercentile, median and percentile aggregate functions for PostgeSQL
====================================================================================================

[](#laravel-eloquent-withmedian-withpercentile-median-and-percentile-aggregate-functions-for-postgesql)

The package provides several aggregate functions that work in the same way as `withAvg()`, `withMax()`

[![Eloquent withMedian, withPercentile](https://github.com/abordage/eloquent-percentile/raw/master/docs/images/eloquent-with-median-percentile.png?raw=true)](https://github.com/abordage/eloquent-percentile/blob/master/docs/images/eloquent-with-median-percentile.png?raw=true)

[ ![Packagist Version](https://camo.githubusercontent.com/374b828eb0f7b49f94b9a1024ce0ae1c60d4502b5c5e7a1c8286a23073af3a19/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61626f72646167652f656c6f7175656e742d70657263656e74696c65)](https://packagist.org/packages/abordage/eloquent-percentile "Packagist version")[ ![Coverage Status](https://camo.githubusercontent.com/1b9a499cbb70ef23a9b48ae4d451b8be045fcae978db50e532b5fdf8f3530f35/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c73436f7665726167652f6769746875622f61626f72646167652f656c6f7175656e742d70657263656e74696c65)](https://coveralls.io/github/abordage/eloquent-percentile "Coverage Status")[ ![GitHub Tests Status](https://camo.githubusercontent.com/ea404d651498c059ec9e5195fd2bf0440c8a14c9c93ad812fd9a15b38751c2b6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f61626f72646167652f656c6f7175656e742d70657263656e74696c652f74657374732e796d6c3f6c6162656c3d7465737473)](https://github.com/abordage/eloquent-percentile/actions/workflows/tests.yml "GitHub Tests Status")[ ![GitHub Code Style Status](https://camo.githubusercontent.com/fe018f11cfea56fdd21097fb3dca5c38dc61c7ae4a0be7562eb3c168e0a26b6d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f61626f72646167652f656c6f7175656e742d70657263656e74696c652f7068702d63732d66697865722e796d6c3f6c6162656c3d636f64652532307374796c65)](https://github.com/abordage/eloquent-percentile/actions/workflows/php-cs-fixer.yml "GitHub Code Style Status")[ ![PHP Version Support](https://camo.githubusercontent.com/76e9b1f62fd6be56641723d8ddd4a35880b5adf1c414b7b34152d449393c4e57/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f61626f72646167652f656c6f7175656e742d70657263656e74696c65)](https://www.php.net/ "PHP version")[ ![License](https://camo.githubusercontent.com/6815cfd44ae139251cd844eb5c1780dc3f1aa233734431258fa70eccc04c8b40/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f61626f72646167652f656c6f7175656e742d70657263656e74696c65)](https://github.com/abordage/eloquent-percentile/blob/master/LICENSE.md "License")

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 11.x / 12.x

Supports:
---------

[](#supports)

- PostgreSQL

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

[](#installation)

You can install the package via composer:

```
composer require abordage/eloquent-percentile
```

Usage
-----

[](#usage)

### Aggregating Related Models

[](#aggregating-related-models)

The method `withMedian()` will place a `{relation}_median_{column}` attribute on your resulting models:

```
use App\Models\Post;

$posts = Post::withMedian('comments', 'votes')->get();

foreach ($posts as $post) {
    echo $post->comments_median_votes;
}
```

The method `withPercentile()` will place a `{relation}_percentile{percentile*100}_{column}` attribute on your resulting models:

```
use App\Models\Post;

$posts = Post::withPercentile('comments', 'votes', 0.85)->get();

foreach ($posts as $post) {
    echo $post->comments_percentile85_votes;
}
```

### Retrieving Aggregates

[](#retrieving-aggregates)

When interacting with Eloquent models, you may also use the `percentile` and `median` aggregate methods. As you might expect, these methods return a scalar value instead of an Eloquent model instance:

```
$median = Comment::where('active', 1)->median('votes');

$percentile95 = Comment::where('active', 1)->percentile('votes', 0.95);
```

Automatic PHPDocs for models
----------------------------

[](#automatic-phpdocs-for-models)

If you are using the [ide-helper](https://github.com/barryvdh/laravel-ide-helper) you can describe the attributes with the [Model Hooks](https://github.com/barryvdh/laravel-ide-helper#model-hooks). For example:

```
