ttpn18121996/historical-records

以简单的方式记录影响数据库的活动历史。

v1.1.6 2024-05-30 10:22 UTC

This package is auto-updated.

Last update: 2024-09-30 11:26:07 UTC


README

通用

以简单的方式记录影响数据库的活动历史。

PHP v8.2

Laravel v11.x

安装

使用composer安装

composer require ttpn18121996/historical-records

接下来,使用 historical-records:install 命令发布 HistoricalRecords 的资源

php artisan historical-records:install

保存历史记录

/*
id: 1
name: Trinh Tran Phuong Nam
email: ttpn18121996@example.com
*/
$user = auth()->user();

$historyRepository = app(\HistoricalRecords\Contracts\HistoryRepository::class);
$history = $historyRepository->saveHistory(
    userId: $user->id,
    tableName: 'users',
    keyword: 'create',
    payload: ['id' => 2, 'name' => 'Minh Nguyet', 'email' => 'minhnguyet@example.com'],
);

echo sprintf(__('history.'.$history->table_name.'.'.$history->keyword.'.action'), $user->name);
// Trinh Tran Phuong Nam has created a user.

覆盖历史仓库

use App\Repositories\HistoryRepository;
use HistoricalRecords\Contracts\HistoryRepository as HistoryRepositoryContract;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(HistoryRepositoryContract::class, HistoryRepository::class);
    }
}

历史清理

运行命令删除30天以上的历史记录

php artisan historical-records:cleanup

如果您想指定超过多少天清除历史记录,请传递选项 --time=。有效值

<number><d|days|m|months|y|years>
#Ex:
7d|7days => 7 days
1m|1months => 1 month
1y|1years => 1 year
php artisan historical-records:cleanup --time=14days
#OR
php artisan historical-records:cleanup -t 14d

显示用户操作和区域

我们支持1套语言来在 config/en/historical.php 文件中显示用户操作。

return [
    'users' => [
        'create' => [
            'title' => 'Create',
            'action' => '%s has created a user.',
        ],
        'update' => [
            'title' => 'Update',
            'action' => '%s has updated a user.',
        ],
        'delete' => [
            'title' => 'Delete',
            'action' => '%s has deleted a user.',
        ],
        'destroy' => [
            'title' => 'Force delete',
            'action' => '%s has hard deleted a user.',
        ],
        'restore' => [
            'title' => 'Restore',
            'action' => '%s has restored a user.',
        ],
        'login' => [
            'title' => 'Login',
            'action' => '%s has logged in.',
        ],
        'change_password' => [
            'title' => 'Change password',
            'action' => '%s has changed the login account password.',
        ],
        'update_profile' => [
            'title' => 'Update profile',
            'action' => '%s has updated the profile.',
        ],
        'email_verification' => [
            'title' => 'Email verification',
            'action' => '%s has verified the email.',
        ],
    ],
];

模型支持用户操作方法。我们可以依赖语言文件来显示用户操作。

假设我们有历史信息

/*
User [
    'id' => 1
    'name' => 'John Doe'
]
History [
    'table_name' => 'users',
    'ketword' => 'create'
    'user_id' => 1
]
*/
$history = History::first();

echo $history->action_for_trans;

// historical.users.create.action
// :name has created a user.

__($history->action_for_trans, ['name' => $history->user->name]);

// John Doe has created a user

配置

配置参数将存储在文件 config/historical-records.php 中。

历史保留期

您可以配置清理的历史周期。默认情况下,历史记录将保留90天内。

return [
    'history_expires' => 90, // days
    ...
];

将保存历史记录的设备名称

您可以为保存历史记录配置设备名称。

return [
    ...
    'device_name' => [ // device name that will be saved
        'phone' => 'phone',
        'tablet' => 'tablet',
        'desktop' => 'desktop',
        'default' => 'unknown',
    ],
];