minex/laravel-change-history

记录对模型执行变更的包

dev-master 2024-07-22 13:28 UTC

This package is auto-updated.

Last update: 2024-09-22 13:49:45 UTC


README

记录对模型执行变更的包。

安装

composer require minex/laravel-change-history

包提供者是自动发现的。

要发布包迁移,使用

php artisan vendor:publish --tag="lch-migrations"

之后运行迁移

php artisan migrate

要更改配置,使用以下命令发布文件

php artisan vendor:publish --tag="lch-config"

使用方法

要为模型记录变更,请为其添加HasChangeHistory特性

use Minex\LaravelChangeHistory\Traits\HasChangeHistory;
use Illuminate\Database\Eloquent\Model;

class SomeModel extends Model {
    use HasChangeHistory;

    /**
     * Attributes in $hidden array will be replaced to **** in history record.
     *
     * @var array
     */
    protected $hidden = ['password', 'other_field'];
}

注意,历史记录基于模型事件,不会通过查询操作触发

示例

#This will NOT save a change record
SomeModel::where('is_active', true)->update([
    'is_active' => false,
]);

#This will save a change record
$model = SomeModel::find(1);
$model->is_active = true;
$model->save();

配置

如果您想忽略历史记录中的某些属性,请使用

use Minex\LaravelChangeHistory\Traits\HasChangeHistory;
use Illuminate\Database\Eloquent\Model;

class SomeModel extends Model {
    use HasChangeHistory;

    public array $lch_ignore_attrs = ['updated_at', 'deleted_at'];
}