LaravelPackages.net
Acme Inc.
Toggle sidebar
bensampo/laravel-count-totals

Elegantly count totals

47
30
1.0
About bensampo/laravel-count-totals

bensampo/laravel-count-totals is a Laravel package for elegantly count totals. It currently has 30 GitHub stars and 47 downloads on Packagist (latest version 1.0). Install it with composer require bensampo/laravel-count-totals. Discover more Laravel packages by bensampo or browse all Laravel packages to compare alternatives.

Last updated

About

Inspired by Jonathan Reinink's post about Calculating totals in Laravel using conditional aggregates I've created an elegant way to grab multiple totals in an efficient way.

Please see the post for details about what this package aims to solve.

Install

composer require bensampo/laravel-count-totals

Usage

Given the following subscribers table structure:

name | status |---|---| Adam Campbell | confirmed Taylor Otwell | confirmed Jonathan Reinink | bounced Adam Wathan | cancelled

$totals = Subscriber::countTotals([
    ['status' => 'confirmed'],
    ['status' => 'cancelled'],
    ['name' => 'Jonathan Reinink'],
]);

$totals->confirmed // 2
$totals->cancelled // 1
$totals->jonathanReinink // 1

You may also use the DB facade:

$totals = DB::table('subscribers')->countTotals([
    ['status' => 'confirmed'],
    ['status' => 'cancelled'],
    ['name' => 'Jonathan Reinink'],
]);

Star History Chart