New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

abordage/eloquent-percentile

Laravel Eloquent withMedian(), withPercentile(), median() and percentile() aggregate functions
3,093 6
Install
composer require abordage/eloquent-percentile
Latest Version:2.1.0
PHP:^8.2
Laravel:11 · 12 · 13
Older releases:8 · 9 · 10
License:MIT
Last Updated:Oct 20, 2026
Links: GitHub  ·  Packagist
Maintainer: abordage

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

Packagist Version Coverage Status GitHub Tests Status GitHub Code Style Status PHP Version Support License

Requirements

  • PHP 8.2+
  • Laravel 12.x / 13.x

Supports:

  • PostgreSQL

Installation

You can install the package via composer:

composer require abordage/eloquent-percentile

Usage

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

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

If you are using the ide-helper you can describe the attributes with the Model Hooks. For example:

<?php

namespace App\Support\IdeHelper;

use App\Models\Post;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
use Illuminate\Database\Eloquent\Model;

class PostHook implements ModelHookInterface
{
    public function run(ModelsCommand $command, Model $model): void
    {
        if (!$model instanceof Post) {
            return;
        }

        $command->setProperty('comments_median_votes', 'float|null', true, false);
        $command->setProperty('comments_percentile80_votes', 'float|null', true, false);
        $command->setProperty('comments_percentile95_votes', 'float|null', true, false);
    }
}

Testing

Before running the tests, rename the phpunit.xml.dist to phpunit.xml and specify your database connection settings:

<php>
    <env name="DB_CONNECTION_POSTGRES" value="pgsql"/>
    <env name="DB_HOST_POSTGRES" value="postgres"/>
    <env name="DB_PORT_POSTGRES" value="5432"/>
    <env name="DB_DATABASE_POSTGRES" value="eloquent_percentile_test"/>
    <env name="DB_USERNAME_POSTGRES" value="default"/>
    <env name="DB_PASSWORD_POSTGRES" value="secret"/>
</php>

Next run:

composer test:all

or

composer test:phpunit
composer test:phpstan
composer test:phpcsf

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

Please review our security policy on how to report security vulnerabilities.

Feedback

Find a bug or have a feature request? Open an issue, or better yet, submit a pull request - contribution welcome!

Credits

Thanks to

The original idea comes from the tailslide-php, so many thanks to its author!

License

The MIT License (MIT). Please see License File for more information.

Related Packages

workos/workos-php-laravel

WorkOS PHP Library for Laravel

2,269,475 39
abordage/laravel-og-images

Generate Open Graph images (og:image, twitter:image, vk:image) for each site pag...

2,791 12
ip2location/ip2location-laravel

Lookup for visitor's IP information, such as country, region, city, coordinates,...

619,959 79
aws/aws-sdk-php-laravel

A simple Laravel 9/10/11/12/13 service provider for including the AWS SDK for PH...

42,050,986 1,714

Version History

Version Released PHP Laravel License
2.1.0 ^8.2 ^11.0 || ^12.0 || ^13.0 MIT
2.0.0 ^8.2 ^11.0 || ^12.0 MIT
1.0.1 >=7.4 ^8.0 || ^9.0 || ^10.0 || ^11.0 MIT
1.0.0 >=7.4 ^8.0 || ^9.0 || ^10.0 MIT
0.1.3 >=7.4 ^8.0 || ^9.0 || ^10.0 MIT