zaimealabs/metrics

The ZaimeaLabs Metrics package.

Fund package maintenance!
Custura

1.1 2025-03-25 10:24 UTC

This package is auto-updated.

Last update: 2025-03-26 09:25:28 UTC


README

Metrics

Metrics for your Model.

Metrics Tests License

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

Donate

Usage

Set your enums in our config

    php artisan vendor:publish --tag=metric

Add HasMetrics trait to your model

    use ZaimeaLabs\Metrics\HasMetrics;

    class User extends Authenticable
    {
        use HasMetrics;
    }

Run migrations to create matrics table in your database

    php artisan migrate

With Config enums

Increment

    $user->incrementMetric(config('metric.enums.Logins'), 1);

Decrement

    $user->decrementMetric(config('metric.enums.Logins'), 1);

Get value

    $user->metrics()->where('name', 'logins')->value('value');

Or use with Enums

Create your enum

    enum UserMetric: string
    {
        case Logins = 'logins';
    }

Increment

    $user->incrementMetric(UserMetric::Logins->value, 1);

Decrement

    $user->decrementMetric(UserMetric::Logins->value, 1);

Get value

    $user->metrics()->where('name', 'logins')->value('value');

Don't increment/decrement with month,year

    $user->incrementMetric(UserMetric::Logins->value, 1, /*withDate*/ false);

    $user->decrementMetric(UserMetric::Logins->value, 1, /*withDate*/ false);

Increment/decrement with specific month,year

    $user->incrementMetric(UserMetric::Logins->value, /*withDate*/ 1, true, /*month*/ 04, /*year*/ 2023, /*day*/ 01);

    $user->decrementMetric(UserMetric::Logins->value, /*withDate*/ 1, true, /*month*/ 04, /*year*/ 2023, /*day*/ 01);