debuqer/eloquent-memory

让eloquent记住其当前的数据状态

dev-main 2023-08-20 11:56 UTC

This package is auto-updated.

Last update: 2024-09-07 12:04:04 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Eloquent memory为你提供了一个基于Laravel模型的时光机,可以穿越模型的当前状态。

$article = Article::create([
    'name' => 'Women, Life, Freedom',
    'content' => 'Hey this content just added,',
]);

// 5 minutes later

// let's change the content 
$article->update([
    'content' => 'Hey this content just changed,'
]);

// 
// Oops, we have changed our content by mistake, let's go back
$articleBeforeUpdate = $article->getStatyeOf(Carbon::now()->subMinutes(2)); 
$articleBeforeUpdate->save(); // will rollback the content of article to the 1 minute ago

安装

您可以通过composer安装此包

composer require debuqer/eloquent-memory

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="eloquent-memory-migrations"
php artisan migrate

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

php artisan vendor:publish --tag="eloquent-memory-config"

这是发布配置文件的内容

return [
    'changes' => [
        'model-updated' => ModelUpdated::class,
        'model-created' => ModelCreated::class,
        'model-deleted' => ModelDeleted::class,
    ],
    'drivers' => [
        'default' => 'eloquent',

        'eloquent' => [
            'class_name' => \Debuqer\EloquentMemory\Repositories\Eloquent\EloquentTransitionPersistDriver::class,
            'connection' => 'default',
        ],
    ],
];

用法

为了强制模型跟踪其状态,必须在模型类中使用CanRememberStates特质

use Debuqer\EloquentMemory\CanRememberStates;

class Post extends Model 
{
    use CanRememberStates;
}

模型将它们的状态记录在数据库中,可以通过getStateOf方法检索状态

$oldArticle = Article::find(5)->getStateOf(Carbon::now()->subMinutes(5));

路线图

  1. 修复迁移,使subject_key应该作为数组提供

  2. 修复迁移以通过laravel发布

  3. 没有存储日期记录字段

测试

composer test

变更日志

此包处于开发模式,不建议在生产环境中使用

贡献

有关详细信息,请参阅CONTRIBUTING

安全漏洞

请审查我们的安全策略,了解如何报告安全漏洞

致谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。