kuncen/audittrails

在 Laravel 应用中记录交易数据的审计跟踪

v1.1.1 2024-09-14 07:06 UTC

This package is not auto-updated.

Last update: 2024-09-28 07:19:18 UTC


README

安装

Laravel 活动日志需要 laravel 7 或更高版本以及 php 7.3+

composer require kuncen/audittrails

然后添加 Kuncen\Audittrails\AudittrailsServiceProvider::class 到 config/app.php 或 laravel 11 的 bootstrap/providers.php

 'providers' => ServiceProvider::defaultProviders()->merge([
        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
         ...
         ...
         Kuncen\Audittrails\AudittrailsServiceProvider::class,
 ])->toArray(),

配置

安装活动日志后,必须使用命令发布其配置

php artisan vendor:publish --provider="Kuncen\Audittrails\AudittrailsServiceProvider"

之后,活动日志将在您的应用程序中创建表以存储交易数据。这就是为什么您需要 migrate 数据库的原因

php artisan migrate

使用方法

此包自动保存所有交易活动,如保存、更新、删除、登录和登出。但在使用之前,您必须在模型上添加 LogTransaction 特性,如下所示

use Laravel\Sanctum\HasApiTokens;
use Kuncen\Audittrails\LogTransaction;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, LogTransaction;
}

设置外键值,如果您想添加外键的引用值,请在模型中添加此内容

    protected $setForeignValues = [
        [
            "foreign"         => "role_id", //foreign key in your models
            "reference_table" => "role", //reference table of your foreign key
            "reference_table_primary" => "role_id", //primary key of your reference table (not required default value is 'id')
            "target_value"    => "nama_role" //this is the name of a column that you want to put on log
        ],

        //repeat add the array if foreign key more than 1
        [
            "foreign"         => "other_foreign",
            "reference_table" => "some_table",
            "target_value"    => "some_target_column"
        ]
    ];

默认情况下,如果您在模型上写入此内容,它将替换外键 id 为您在此数组中设置的外键值。但如果您仍然想保留外键 id 并添加外键值而不替换它,您可以使用 hideForeignId(false)

    $data = User::hideForeignId(false)->find(1);
    $data->name = "Dummy Example";
    $data->email = "dummy@gmail.com";
    $data->password = bcrypt("examplepassword");
    $data->update();

所有在登录之前进行的交易数据(如忘记密码和注册)可能将存储在 activity_log 表的用户_id 列中的空值。如果您仍然需要在交易中需要用户身份,您可以使用 withAuth() 将用户 id 强制转换为,如下所示

$data = User::withAuth(17)->find(1);
$data->name = "Dummy Example";
$data->email = "dummy@gmail.com";
$data->password = bcrypt("examplepassword");
$data->update();

禁用某些函数的日志记录

$data = User::disableAudit(true)->find(1);
$data->name = "Dummy Example";
$data->email = "dummy@gmail.com";
$data->password = bcrypt("examplepassword");
$data->update();

如果您想让应用程序中的所有交易记录为进入菜单或页面,而不是保存()、更新()、删除()、登录和登出等操作,您可以将 setActivityLog() 辅助函数添加到您创建的函数中

setActivityLog(
    "description about this function/page/menu (opsional default is null)",
    user id (opsional if you need user identity for your function default is null),
    http method or action (opsional by default value is "READ"),
    "New values what you need to store it (opsional)",
    "Old values what you need to store it (opsional)"
);