定制 lulzshadowwalker/laravel-metrics 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 type
  • today() — events created today
  • period(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
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固