thoth-pharaoh/operation-record

操作记录-收集器

v3.0.2 2023-02-24 11:37 UTC

README

版本匹配

安装

您可以使用 composer 进行安装

composer require thoth-pharaoh/operation-record

迁移 operation_records 数据表

php artisan migrate

导出 Migration

php artisan vendor:publish --tag=operation-record-database --force

导出 Config

php artisan vendor:publish --tag=operation-record-config --force

使用方法

使用模型关联

首先在要使用的模型中引入 HasOperationRecord trait

use Pharaoh\OperationRecord\Traits\HasOperationRecord;

class User extends Model
{
    use HasOperationRecord;
}
  • 操作者 Model 修改 操作对象 Model 记录操作
$user = new \App\Models\User;
$user->operating($subject, $funcKey, $action, $old, $new, $ip);

$action 动作参数请参考 config.operation_record.action 内容

  • 操作对象 Model 被操作者 Model 修改 记录操作
$post = new \App\Models\Post;
$post->operatedBy($operator, $funcKey, $action $old, $new, $ip);
  • 操作者 Model 获取修改记录
$user = new \App\Models\User;
$records = $user->getOperatorRecords()->get();
  • 操作对象 Model 获取被修改记录
$post = new \App\Models\Post;
$records = $post->getSubjectRecords()->get();

使用 Facade

首先引入门面

use Pharaoh\OperationRecord\Facades\OperationRecord;
  • 创建一条操作记录
OperationRecord::create($operatorId, $operatorType, $subjectId, $subjectType, $funcKey, $action ,$old, $new $ip);
  • 创建一条操作记录(使用队列作业的方式)
OperationRecord::dispatch($operatorId, $operatorType, $subjectId, $subjectType, $funcKey, $action, $old, $new $ip);

队列名称以 config/operation_record.php 中设置的名字为主

  • 搜索操作记录
$params = [
    'operator' => [
        'id' => 1,
        'type' => User::class,
    ],
    'subject' => [
        'id' => [2, 3],
        'type' => Post::class,
    ],
    .....
];

OperationRecord::find($params);

$params 内容可以自定义选择搭配,可选项如下

  • 删除 $datetime 之前的操作记录
$dataTime = '2020-07-30 00:00:00'

OperationRecord::removeBefore($dataTime);
  • 删除 $datetime 之后的操作记录
$dataTime = '2020-07-30 00:00:00'

OperationRecord::removeAfter($dataTime);
  • 清空操作记录
OperationRecord::truncate();