定制 asif/laravel-model-logs 二次开发

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

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

asif/laravel-model-logs

Composer 安装命令:

composer require asif/laravel-model-logs

包简介

README 文档

README

The laravel-model-logs package provides an easy way to track changes made to your Eloquent models and log them into an audit_logs table. It allows you to keep a history of all the modifications made to your models, making it useful for auditing purposes, user activity tracking, and more.

Installation

You can install the package via Composer by running the following command:

composer require asif/laravel-model-logs

After installing the package, Laravel will automatically discover the service provider.

Usage

Step 1: Set up the Database

Before using the package, you need to create the audit_logs table in your database. You can use the provided migration file to create the table by running the migration:

php artisan migrate

Step 2: Add the LogChanges Trait to Your Models

To start tracking changes on your models, add the LogChanges trait to each model you want to monitor. The trait provides the necessary functionality to automatically log changes whenever the model is created, updated, or deleted.

use Asif\LaravelModelLogs\Traits\LogChanges;
use Illuminate\Database\Eloquent\Model;

class YourModel extends Model
{
    use LogChanges;

    // Rest of your model code...
}

Step 3: Customize Tracked Events (Optional)

By default, the LogChanges trait tracks all events: 'created', 'updated', and 'deleted'. However, you can customize the tracked events for each model by adding a $trackedEvents property to the model. If the property is not defined, the package will track all events.

use Asif\LaravelModelLogs\Traits\LogChanges;
use Illuminate\Database\Eloquent\Model;

class YourModel extends Model
{
    use LogChanges;

    /**
     * Customize the tracked events for this model.
     *
     * @var array<string>
     */
    protected $trackedEvents = ['created', 'updated'];

    // Rest of your model code...
}

Step 4: Define Loggable Fields (Optional)

You can further customize the fields that should be logged for each model by adding a $loggable property to the model, containing an array of field names that you want to track. If the property is not provided, the package will log all the fields.

use Asif\LaravelModelLogs\Traits\LogChanges;
use Illuminate\Database\Eloquent\Model;

class YourModel extends Model
{
    use LogChanges;

    /**
     * The attributes that should be logged in the audit log.
     *
     * @var array<string>
     */
    protected $loggable = ['name', 'email'];

    // Rest of your model code...
}

License

The laravel-model-logs package is open-sourced software licensed under the MIT License.

asif/laravel-model-logs 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-07-30