shipu/watchable

v1.0.0 2022-06-30 06:01 UTC

This package is auto-updated.

Last update: 2024-09-20 20:05:55 UTC


README

Watchable 是一个 Laravel 扩展包,您可以在应用程序中轻松地捕获 Laravel 模型事件和活动日志。

use Shipu\Watchable\Traits\WatchableTrait;
onModelCreating
onModelCreated
onModelUpdating
onModelUpdated

该包将所有活动存储在 activity_logs 表中。以下是如何使用它的示例

activity()->log('Look, I logged something');

您可以使用 Shipu\Watchable\Models\Activity 模型检索所有活动。

Activity::all();

以下是一个更高级的示例

activity()
   ->on($anEloquentModel)
   ->data($storeWhatYouWantTo)
   ->log('Look, I logged something');
   
$lastLoggedActivity = Activity::all()->last();

$lastLoggedActivity->model; //returns an instance of an eloquent model
$lastLoggedActivity->causer; //returns an instance of your user model
$lastLoggedActivity->remarks; //returns 'Look, I logged something'

以下是一个关于 事件记录 的示例。

$user->name = 'updated name';
$user->save();

//updating the newsItem will cause the logging of an activity
$activity = Activity::all()->last();

$activity->remarks; //returns 'User Updated'
$activity->model; //returns the instance of NewsItem that was created

调用 $activity->changes 将返回此数组

[
   'new' => [
        'name' => 'updated name',
        'text' => 'Lorum',
    ],
    'old' => [
        'name' => 'original name',
        'text' => 'Lorum',
    ],
];

安装

您可以通过 composer 安装该包

composer require shipu/watchable

您可以选择使用以下命令发布配置文件:

php artisan vendor:publish --provider="Shipu\Watchable\WatchableServiceProvider" --tag="shipu-watchable-config"

这是已发布配置文件的内容

return [
    'audit_columns' => [
        'creator_column' => 'creator',
        'editor_column' => 'editor',
        'default_active' => false,
    ],
    'activity_log' => [
        'model' => \Shipu\Watchable\Models\Activity::class
    ]
];

您可以使用以下命令发布迁移:

php artisan vendor:publish --provider="Shipu\Watchable\WatchableServiceProvider" --tag="shipu-watchable-migrations"

注意:默认迁移假设您正在使用整数作为模型 ID。如果您正在使用 UUID 或其他格式,请在继续之前调整已发布迁移中 subject_id 和 causer_id 字段的格式。

发布迁移后,您可以通过运行迁移来创建 activity_logs

php artisan migrate