isaevdimka/laravel-model-changes-history
Composer 安装命令:
composer require isaevdimka/laravel-model-changes-history
包简介
Model changes history for laravel
README 文档
README
Records the changes history made to an eloquent model.
Quick start
Your model must have an id field!
composer require isaevdimka/laravel-model-changes-history
php artisan vendor:publish --tag="model-changes-history"
php artisan migrate
Note: this library use database storage as default.
Installation
composer require isaevdimka/laravel-model-changes-history
The package is auto discovered.
To change the config, publish it using the following command:
php artisan vendor:publish --provider="ModelChangesHistory\Providers\ModelChangesHistoryServiceProvider" --tag="config"
You can use three ways for record changes: 'storage' => 'database', 'file' or 'redis'
If you want to use database storage, you must publish the migration file, run the following artisan commands:
php artisan vendor:publish --provider="ModelChangesHistory\Providers\ModelChangesHistoryServiceProvider" --tag="migrations"
php artisan migrate
Use this environment to manage library:
# Global recorgin model changes history RECORD_CHANGES_HISTORY=true # Default storage for recorgin model changes history MODEL_CHANGES_HISTORY_STORAGE=database
Usage
Add the trait to your model class you want to record changes history for:
use 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']; }
Your model now has a relation to all the changes made:
$testModel->latestChange(); 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" ] ... }
Getting all changes history:
$testModel->historyChanges(); Illuminate\Database\Eloquent\Collection { #items: array:3 [ 0 => ModelChangesHistory\Models\Change {...} 1 => ModelChangesHistory\Models\Change {...} 2 => ModelChangesHistory\Models\Change {...} ... }
If you use database storage you can also use morph relations to Change model:
$testModel->latestChangeMorph(); $testModel->historyChangesMorph();
Clearing changes history:
$testModel->clearHistoryChanges();
Get an independent changes history:
use 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
Getting model changer:
// Return Authenticatable `changer_type` using HasOne relation to changer_type and changer_id $changer = $latestChange->changer;
If you use database storage you can use Change model as:
use 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();
Clearing changes history using console:
php artisan changes-history:clear
You can use it in Kelner:
protected function schedule(Schedule $schedule) { $schedule->command('changes-history:clear')->monthly(); }
isaevdimka/laravel-model-changes-history 适用场景与选型建议
isaevdimka/laravel-model-changes-history 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 02 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「model」 「laravel」 「history」 「changes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 isaevdimka/laravel-model-changes-history 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 isaevdimka/laravel-model-changes-history 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 isaevdimka/laravel-model-changes-history 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Laravel 5 - Repositories to the database layer
This package provides an Address Model, Migration and Factory. It also provides the traits HasBillingAddres and HasPublicAddress
The YADM migrations
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-01