zaimealabs/aggregate

The ZaimeaLabs Aggregate package.

1.0.1 2024-12-21 09:43 UTC

This package is auto-updated.

Last update: 2025-02-21 10:06:01 UTC


README

Aggregate

Generate aggregate for your models to generate charts or reports in your application.

Aggregate Tests License

Hey 👋 thanks for considering making a donation, with these donations I can continue working to contribute to ZaimeaLabs projects.

Donate

Usage

To generate a aggregate for your model, import the ZaimeaLabs\Aggregate\Aggregate class and pass along a model or query. ::model() come as it is. ::query() allow you to use additional filter like where().

// Totals per month
    $model = Aggregate::model(User::class)
        ->between(
            start: now()->startOfYear(),
            end: now()->endOfYear(),
        )
        ->perMonth()
        ->count();

// Count users register per year, results are grouped per month
    $query = Aggregate::query(User::whereNotNull('email_verified_at'))
        ->between(
            start: now()->startOfYear()->subYears(10),
            end: now()->endOfYear(),
        )
        ->perYear()
        ->count();

// Average user calendar activity where record type is work with a over a span of 11 years, results are grouped per year
    $query = Aggregate::query(Record::where('type', 'work'))
        ->dateColumn('scheduled_at')
        ->between(
            start: now()->startOfYear()->subYears(10),
            end: now()->endOfYear())
        ->perYear()
        ->sumTime('duration');

Date Column

If your column is not created_at just use dateColumn('pointed_date_column')

->dateColumn('pointed_date_column')

Aggregates

You can use the following aggregates:

Intervals

You can use the following aggregates intervals: