peters-development/laravel-model-logger 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

peters-development/laravel-model-logger

Composer 安装命令:

composer require peters-development/laravel-model-logger

包简介

Lightweight polymorphic model activity logging for Laravel.

README 文档

README

Lightweight polymorphic model activity logging for Laravel.

  • Zero-config for simple appscomposer require, php artisan migrate, start logging.
  • Channels for advanced apps — route logs to different tables, connections, or morph key types (numeric/uuid/ulid) when multiple packages or databases are involved.

Installation

composer require peters-development/laravel-model-logger
php artisan migrate

The service provider is auto-discovered. The default migration creates a model_logs table on your default connection.

Basic usage

Add the HasModelLogs trait to any Eloquent model:

use PetersDevelopment\ModelLogger\HasModelLogs;

class Order extends Model
{
    use HasModelLogs;
}

Log activity:

$order->log('Order was shipped');
$order->log('Payment received', ['amount' => 99.99, 'method' => 'ideal']);

$order->logs;        // MorphMany — collection of ModelLog
$log = $order->log('Status changed', ['from' => 'pending', 'to' => 'shipped']);

The authenticated user is automatically recorded. Meta is stored as JSON.

Querying

use PetersDevelopment\ModelLogger\ModelLog;

$log->user;                                // User model that performed the action
$logsByUser = ModelLog::forUser($userId)->get();

Configuration

Publish to customise:

php artisan vendor:publish --tag=model-logger-config

The config is built around channels. Each channel owns its own table, connection, and morph-key type. Simple apps use the single bundled default channel and never touch this:

// config/model-logger.php (default — works without publishing)
return [
    'default' => env('MODEL_LOGGER_DEFAULT_CHANNEL', 'default'),

    'channels' => [
        'default' => [
            'table' => 'model_logs',
            'connection' => null,           // null = Laravel default
            'morph_key_type' => 'numeric',  // 'numeric' | 'uuid' | 'ulid'
            'user_model' => null,           // null = config('auth.providers.users.model')
        ],
    ],
];

Channels (advanced)

Channels let multiple loggers coexist in one app without colliding. Typical use cases:

  • A multi-tenant SaaS that wants landlord auth-events on one connection and tenant business-events on another.
  • A package (e.g. a billing or auth package) that ships its own table-prefixed channel without overwriting the host app's logs.
  • An app with UUID or ULID keyed models that wants properly-sized morph columns.

Declaring a channel

Add a channel entry to your published config:

// config/model-logger.php
return [
    'default' => 'default',

    'channels' => [
        'default' => [
            'table' => 'model_logs',
            'connection' => null,
            'morph_key_type' => 'numeric',
            'user_model' => null,
        ],
        'auth' => [
            'table' => 'auth_logs',
            'connection' => 'landlord',
            'morph_key_type' => 'numeric',
            'user_model' => \App\Models\User::class,
        ],
    ],
];

Routing a model's logs to a channel

Override getModelLogChannel():

class User extends Authenticatable
{
    use HasModelLogs;

    public function getModelLogChannel(): string
    {
        return 'auth';
    }
}

$user->log(...) and $user->logs now read/write the auth channel's table on the landlord connection.

Creating the channel's table

Write a migration on the channel's target connection. The package ships a schema helper so you don't have to copy column definitions:

use PetersDevelopment\ModelLogger\Schema\ModelLogsBlueprint;

return new class extends Migration {
    public function up(): void
    {
        Schema::connection('landlord')->create('auth_logs', function (Blueprint $table) {
            ModelLogsBlueprint::columns($table, morphKeyType: 'numeric');
        });
    }

    public function down(): void
    {
        Schema::connection('landlord')->dropIfExists('auth_logs');
    }
};

Packages that ship their own channel

A consuming package can register its own channel from its service provider so it never clobbers the host app's default:

public function register(): void
{
    config()->set('model-logger.channels.mypackage', [
        'table' => 'mypackage_logs',
        'connection' => config('mypackage.database.connection'),
        'morph_key_type' => 'numeric',
    ]);
}

Then every model in the package overrides getModelLogChannel() to return 'mypackage'.

morph_key_type

Value When to use
numeric Default. Bigint auto-increment IDs — fits ~95% of Laravel apps.
uuid Models keyed by UUID.
ulid Models keyed by ULID (Laravel's HasUlids trait).

A single app can mix all three by using one channel per key type.

License

MIT

peters-development/laravel-model-logger 适用场景与选型建议

peters-development/laravel-model-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 57 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 peters-development/laravel-model-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 peters-development/laravel-model-logger 我们能提供哪些服务?
定制开发 / 二次开发

基于 peters-development/laravel-model-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 57
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 40
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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