antonrom00 / laravel-model-changes-history
laravel 的模型更改历史
dev-master
2021-11-04 14:32 UTC
Requires
- php: >=7.2
- ext-json: *
- laravel/framework: >=5.8
- predis/predis: ^1.1
Requires (Dev)
- orchestra/testbench: ^3.5|^4.2
This package is auto-updated.
Last update: 2024-09-04 20:52:52 UTC
README
记录对 eloquent 模型所做的更改历史。
快速开始
你的模型必须有 id
字段!
composer require antonrom00/laravel-model-changes-history
php artisan vendor:publish --tag="model-changes-history"
php artisan migrate
注意:此库默认使用 database
存储作为存储。
安装
composer require antonrom00/laravel-model-changes-history
此包是自动发现的。
要更改配置,请使用以下命令发布它
php artisan vendor:publish --provider="Antonrom\ModelChangesHistory\Providers\ModelChangesHistoryServiceProvider" --tag="config"
你可以使用三种方式来记录更改: 'storage' => 'database', 'file' 或 'redis'
如果你想要使用 database
存储,你必须发布迁移文件,运行以下 artisan 命令
php artisan vendor:publish --provider="Antonrom\ModelChangesHistory\Providers\ModelChangesHistoryServiceProvider" --tag="migrations"
php artisan migrate
使用此环境来管理库
# Global recorgin model changes history RECORD_CHANGES_HISTORY=true # Default storage for recorgin model changes history MODEL_CHANGES_HISTORY_STORAGE=database
探索更多详细库设置,请查看 配置
用法
将特性添加到您想要记录更改历史的模型类中
use Antonrom\ModelChangesHistory\Traits\HasChangesHistory; use Illuminate\Database\Eloquent\Model; class TestModel extends Model { use HasChangesHistory; /** * The attributes that are mass assignable. * This will also be hidden for changes history. * * @var array */ protected $hidden = ['password', 'remember_token']; }
您的模型现在与所有更改相关联
$testModel->latestChange(); Antonrom\ModelChangesHistory\Models\Change { ... #attributes: [ "model_id" => 1 "model_type" => "App\TestModel" "before_changes" => "{...}" "after_changes" => "{...}" "change_type" => "updated" "changes" => "{ "title": { "before": "Some old title", "after": "This is the new title" }, "body": { "before": "Some old body", "after": "This is the new body" }, "password": { "before": "[hidden]", "after": "[hidden]" } }" "changer_type" => "App\User" "changer_id" => 1 "stack_trace" => "{...}" "created_at" => "2020-01-21 17:34:31" ] ... }
获取所有更改历史
$testModel->historyChanges(); Illuminate\Database\Eloquent\Collection { #items: array:3 [ 0 => Antonrom\ModelChangesHistory\Models\Change {...} 1 => Antonrom\ModelChangesHistory\Models\Change {...} 2 => Antonrom\ModelChangesHistory\Models\Change {...} ... }
如果您使用 database
存储,您还可以使用形态关系来 Change
模型
$testModel->latestChangeMorph(); $testModel->historyChangesMorph();
清除更改历史
$testModel->clearHistoryChanges();
获取独立的更改历史
use Antonrom\ModelChangesHistory\Facades\HistoryStorage; ... $latestChanges = HistoryStorage::getHistoryChanges(); // Return collection of all latest changes $latestChanges = HistoryStorage::getHistoryChanges($testModel); // Return collection of all latest changes for model $latestChange = HistoryStorage::getLatestChange(); // Return latest change $latestChange = HistoryStorage::getLatestChange($testModel); // Return latest change for model HistoryStorage::deleteHistoryChanges(); // This will delete all history changes HistoryStorage::deleteHistoryChanges($testModel); // This will delete all history changes for model
获取模型更改者
// Return Authenticatable `changer_type` using HasOne relation to changer_type and changer_id $changer = $latestChange->changer;
如果您使用 database
存储,您可以使用 Change
模型作为
use Antonrom\ModelChangesHistory\Models\Change; // Get the updates on the given model, by the given user, in the last 30 days: Change::query() ->whereModel($testModel) ->whereChanger($user) ->whereType(Change::TYPE_UPDATED) ->whereCreatedBetween(now()->subDays(30), now()) ->get();
使用控制台清除更改历史
php artisan changes-history:clear
您可以在 Kelner
中使用它
protected function schedule(Schedule $schedule) { $schedule->command('changes-history:clear')->monthly(); }