承接 temistocle1998/laravel-model-tracker 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

temistocle1998/laravel-model-tracker

Composer 安装命令:

composer require temistocle1998/laravel-model-tracker

包简介

A Laravel package for tracking model changes, including old values, new values, and the user responsible for the changes

README 文档

README

Laravel Logo

Laravel Model Tracker

Latest Stable Version Total Downloads License

A lightweight Laravel package to track changes in your models. Automatically logs all changes, including old values, new values, and the user responsible for the changes.

Features

  • Automatically tracks changes in any Eloquent model.
  • Stores old and new values of updated fields.
  • Logs the user who made the change (if authenticated).
  • Provides easy integration with a simple trait.
  • Configurable to exclude fields from tracking.
  • Includes a migration to store logs in a model_changes table.

Requirements

  • Laravel 8.x or 9.x
  • PHP 7.4 or higher

Installation

  1. Install via Composer:

    composer require temistocle1998/laravel-model-tracker
  2. Publish configuration and migration files:

    php artisan vendor:publish --tag=tracker-config
    php artisan migrate
  3. Add the TracksChanges trait to your model:

    In any model where you want to track changes, simply include the TracksChanges trait:

    use Tracker\Traits\TracksChanges;
    
    class Product extends Model
    {
        use TracksChanges;
    }

Configuration

The package comes with a config file that can be customized to suit your needs. The configuration file can be found at config/tracker.php.

Example Config:

return [
    'enabled' => true, // Enable or disable the tracking
    'exclude_fields' => ['password', 'remember_token'], // Fields you don't want to track
];

Usage

Once the package is set up, any changes made to the models using the TracksChanges trait will automatically be logged. The logs are stored in the model_changes table.

Example of Change Logs

When a model is updated, the following log is created in the model_changes table:

model_type model_id user_id changes created_at
App\Models\Product 1 2 {"name": {"old_value": "Old Name", "new_value": "New Name"}} 2023-01-01 12:00:00

Retrieving Model Changes

You can fetch model changes using the ModelChange model.

use Tracker\Models\ModelChange;

// Get all changes for a specific model
$changes = ModelChange::where('model_type', 'App\Models\Product')
                      ->where('model_id', $productId)
                      ->get();

// Display the changes
foreach ($changes as $change) {
    echo $change->changes;
}

Displaying Changes in Your View

You can also display the changes in your views:

@foreach ($product->changes as $change)
    <p>Field: {{ $change['field'] }} | Old Value: {{ $change['old_value'] }} | New Value: {{ $change['new_value'] }}</p>
@endforeach

Contributing

Feel free to submit a pull request if you'd like to contribute to this package. All contributions are welcome!

License

This package is licensed under the MIT License. See the LICENSE file for details.

temistocle1998/laravel-model-tracker 适用场景与选型建议

temistocle1998/laravel-model-tracker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 12, 最近一次更新时间为 2024 年 09 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-19