Skip to content

Support additional aggregate queries on Eloquent relations

License

Notifications You must be signed in to change notification settings

dbakan/aggregate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Aggregate (WIP)

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 dwightwatson/aggregate. which is based off the work in laravel/framework#25319.

Installation

Usage

Order::withSum(
    'items.id as total_items', 
    'items.price as total_cost', 
    'items.weight as total_weight',
);
Order::withSum([
    'items.id as total_items', 
    'items.price',
    'items.quantity', 
    'items.quantity as downloadable_count' => function($query) {
        $query->where('is_downloadable', true);
    }, 
]);
Order::withAggregate([
    'products as product_names' => \DB::raw('GROUP_CONCAT(name)'),
]);
Order::withAggregate([
    'products' => 'count',
    'products.* AS count_total' => 'count',
    'products.comment AS count_commented' => 'count',
    'products.quantity' => 'sum',
    'products.price as custom_price_aggr' => function($query) {
        $query->select(\DB::raw('SUM(product_orders.quantity*product_orders.price)'));
    },
    'products.discount' => 'MAX',
    'products.quantity as quantity_list' => 'group_concat',
]);

Testing

vendor/bin/phpunit

About

Support additional aggregate queries on Eloquent relations

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%