lulzshadowwalker/laravel-metrics
Composer 安装命令:
composer require lulzshadowwalker/laravel-metrics
包简介
Simple metrics for Laravel applications
README 文档
README
A minimal, no-frills way to record and query historical events on your Eloquent models. Inspired by aarondfrancis/eventable, which does a lot more than this package does. If you need something more full-featured, use that instead.
Installation
composer require lulzshadowwalker/laravel-metrics
Then publish the migration and config, and migrate:
php artisan vendor:publish --tag=metrics-migrations php artisan vendor:publish --tag=metrics-config php artisan migrate
Usage
Add the HasMetrics trait to any model you want to record events against:
use Metrics\Traits\HasMetrics; class Tenant extends Model { use HasMetrics; }
Define your own enum for the events you want to track. This package doesn't ship one, since every app's events are different:
enum Metric: string { case SignedUp = 'signed_up'; case OrderPlaced = 'order_placed'; case HumanEscalated = 'human_escalated'; }
Record events:
$tenant->record(Metric::OrderPlaced, ['channel' => 'web']); // meta is optional $tenant->record(Metric::HumanEscalated);
record() also accepts a plain string if you don't want to use an enum.
Query events:
$tenant->metrics()->type(Metric::OrderPlaced)->today()->count(); $tenant->metrics() ->type(Metric::HumanEscalated) ->period(now()->subDays(30), now()) ->count();
Available scopes
type(string|BackedEnum $type)— filter by event typetoday()— events created todayperiod(Carbon $from, Carbon $to)— events created within an inclusive date range
Extending the Event model
If you need extra columns (a tenant_id, for example), extend the base model:
namespace App\Models; use Metrics\Models\Event as BaseEvent; class Event extends BaseEvent { // }
Add whatever columns you need via a new migration, then point the package at your model in config/metrics.php:
return [ 'model' => \App\Models\Event::class, ];
Every model using HasMetrics will now use App\Models\Event instead of the package's default. This is a single, app-wide setting, not something you configure per model.
Testing
composer test
License
MIT.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-07