zaimealabs / metrics
The ZaimeaLabs Metrics package.
Fund package maintenance!
Custura
Requires
- php: ^8.2
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0
- pestphp/pest: ^2.0|^3.0
README
Metrics for your Model.
Hey 👋 thanks for considering making a donation, with these donations I can continue working to contribute to ZaimeaLabs projects.
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);