elaborate-code / laravel-eloquent-logs
Requires
- php: ^8.1
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2023-10-09 19:42:04 UTC
README
记录你的 Eloquent 模型(created
|updated
|deleted
|软删除
|恢复
|强制删除
)的变更,并关注是谁、如何以及何时进行了变更。
此解决方案易于集成,并对你的项目改动最小:1 个迁移,1 个模型,1 个特质和 1 个外观。
安装
通过 composer 安装此包
composer require elaborate-code/laravel-eloquent-logs
发布迁移
php artisan vendor:publish --tag="eloquent-logs-migrations"
运行迁移
php artisan migrate
发布配置文件(可选)
你可以使用以下命令发布配置文件:
php artisan vendor:publish --tag="eloquent-logs-config"
这是发布配置文件的内容
return [ 'logs_model' => \ElaborateCode\EloquentLogs\Models\EloquentLog::class, 'logs_table' => 'eloquent_logs', 'user' => \App\Models\User::class, ];
这允许你在运行迁移之前重命名 logs_table
。
使用方法
选择一个你想要记录变更的 Eloquent 模型,并为其添加 HasLogs
特质。
namespace App\Models; use Illuminate\Database\Eloquent\Model; class ExampleModel extends Model { use \ElaborateCode\EloquentLogs\Concerns\HasLogs; // ... }
添加该特质后,对模型所做的每个变更都将被记录。
重要警告 来自 Laravel 文档
当通过 Eloquent 发出 批量更新或删除 查询时,受影响的模型将不会触发
saved
、updated
、deleting
和deleted
模型事件。这是因为执行批量更新或删除时,模型实际上从未被检索。
检索日志
你可以使用 eloquentLogs
关系加载模型日志
$example_model->eloquentLogs; $example_model->load('eloquentLogs'); App\Models\ExampleModel::with('eloquentLogs')->find($id);
并且可以直接查询日志
// latest 5 logs with affected models ElaborateCode\EloquentLogs\Models\EloquentLog::with('loggable')->latest()->limit(5)->get()
分组查询
默认情况下,每个模型事件都会导致对日志的操作进行一次查询。
$example_model = ExampleModel::create(['name' => 'foo']); $example_model->update(['name' => 'bar']); $example_model->delete(); // ⚠️ This will result in 3 queries to insert the 3 events logs into the database
你可以通过使用 CacheEloquentLogQueries
外观来提高日志记录过程
use ElaborateCode\EloquentLogs\Facades\CacheEloquentLogQueries; CacheEloquentLogQueries::start(); $example_model = ExampleModel::create(['name' => 'foo']); $example_model->update(['name' => 'bar']); $example_model->delete(); CacheEloquentLogQueries::execute(); // 👍 This will result in 1 query to insert the 3 events logs into the database
外观还包括你可能不需要使用的方法
// Stops caching and empties the cache without queries execution CacheEloquentLogQueries::reset(); // Empties the cache but doesn't stop caching CacheEloquentLogQueries::flushQueries(); // Stops caching until the reuse of start() and doesn't empty the cache CacheEloquentLogQueries::suspend(); // Returns a boolean CacheEloquentLogQueries::isCaching();
忽略事件
你可以在模型实例上通过在 YourModel::$loggableOptions['ignore']
中列出要忽略的事件来指定不记录的事件。
namespace App\Models; use Illuminate\Database\Eloquent\Model; class ExampleModel extends Model { use \ElaborateCode\EloquentLogs\Concerns\HasLogs; public static array $loggableOptions = [ 'ignore' => ['created', 'updated', 'deleted', 'softDeleted', 'forceDeleted', 'restored'], ]; // ... }
静默 Eloquent 事件 [Laravel 东西]
来自种子文件
namespace Database\Seeders; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { use WithoutModelEvents; // Add this trait public function run(): void { // Silent eloquent queries ... } }
从你的代码中的任何地方
\Illuminate\Database\Eloquent\Model::unsetEventDispatcher(); // Silent eloquent queries ... \Illuminate\Database\Eloquent\Model::setEventDispatcher(app(Dispatcher::class)); // ...
有关更多选项,请参阅 Eloquent 文档
替代方案
在 Spatie 慷慨赠予社区的无数个包中,你会发现优秀的 laravel-Alternative 包。
测试
composer test
变更日志
请参阅 CHANGELOG 以了解最近发生了什么变化。
贡献
请参阅 CONTRIBUTING 以获取详细信息。
安全漏洞
请审查 我们的安全策略 了解如何报告安全漏洞。
鸣谢
许可证
MIT 许可证(MIT)。请参阅 许可证文件 以获取更多信息。