karl456/laravel-revisions 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

karl456/laravel-revisions

Composer 安装命令:

composer require karl456/laravel-revisions

包简介

Create multiple revisions of any Eloquent model record along with its underlying relationships

README 文档

README

Unfortunately this package is now discontinued.
Please check out Varbox (Laravel Admin Panel) for this functionality and much more.

Thank you!

Create revisions for any Eloquent model along with its relationships

Build Status StyleCI Scrutinizer Code Quality

Overview

This package allows you to create revisions for any Eloquent model record along with its underlying relationships.

  • When a revision is created, it gets stored inside the revisions database table.
  • Revisions are created automatically on model update, using the updated Eloquent event
  • Revisions can also can be created manually by using the saveAsRevision()
  • When a record is force deleted, all its revisions will also be removed automatically, using the deleted Eloquent event

As already mentioned, this package is capable of revisioning entire relationships alongside the model record.

The cool part is that it's also capable of re-creating the relationships records from ground up, if they were force deleted along the way, during the lifetime of that model record.

Relationship types that can be revisioned: hasOne, morphOne, hasMany, morphMany, belongsToMany, morphToMany

Installation

Install the package via Composer (for Laravel 6.0 and above):

composer require karl456/laravel-revisions

Install the package via Composer (for Laravel 5.8):

composer require karl456/laravel-revisions:3.1.0

Install the package via Composer (for Laravel 5.7 and below):

composer require karl456/laravel-revisions:2.0.0

Publish the config file with:

php artisan vendor:publish --provider="Karl456\Revisions\ServiceProvider" --tag="config"

Publish the migration file with:

php artisan vendor:publish --provider="Karl456\Revisions\ServiceProvider" --tag="migrations"

After the migration has been published you can create the revisions table by running:

php artisan migrate

Setup

Step 1

Your Eloquent models should use the Karl456\Revisions\Traits\HasRevisions trait and the Karl456\Revisions\Options\RevisionOptions class.

The trait contains an abstract method getRevisionOptions() that you must implement yourself.

Here's an example of how to implement the trait:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Karl456\Revisions\Options\RevisionOptions;
use Karl456\Revisions\Traits\HasRevisions;

class YourModel extends Model
{
    use HasRevisions;

    /**
     * Get the options for revisioning the model.
     *
     * @return RevisionOptions
     */
    public function getRevisionOptions(): RevisionOptions
    {
        return RevisionOptions::instance();
    }
}
Step 2

Inside the revisions.php config file, write the full namespace of your User model class for the user_model config key.

By default, this value is the FQN of Laravel's User model class (\App\User). You can also leave this NULL if your application doesn't have the concept of users.

This bit is used by the Karl456\Revisions\Traits\HasRevisions trait to know who created which revisions.

Usage

Fetch revisions

You can fetch a model record's revisions by using the revisions() morph to many relation present on the Karl456\Revisions\Traits\HasRevisions trait.

$model = YourModel::find($id);

$revisions = $model->revisions;
Create revisions (automatically)

Once you've used the Karl456\Revisions\Traits\HasRevisions trait in your Eloquent models, each time you update a model record, a revision containing its original attribute values will be created automatically using the updated Eloquent event:

// model is state 1
$model = YourModel::find($id);

// model is state 2
// a revision containing the model's state 1 is created 
$model->update(...);

Alternatively, you can also store a revision each time you create a new model record, by using the created Eloquent event
(see Customisations)

Create revisions (manually)

If you ever need it, you can also create a revision manually, by using the saveAsRevision() method from the Karl456\Revisions\Traits\HasRevisions trait:

$model = YourModel::find($id);

// a new entry is stored inside the 'revisions' database table
// reflecting the current state of that model record
$model->saveAsRevision();
Rollback to a past revision

You can rollback the model record to one of its past revisions by using the rollbackToRevision() method.

// model is state 1
$model = YourModel::find($id);
$revision = $model->revisions()->latest()->first();

// model is now in state 0
$model->rollbackToRevision($revision);

Customisations

Enable revisioning on create

By default, when creating a new model record, a revision will not be created, because the record is fresh and it's at its first state. However, if you wish to create a revision when creating the model record, you can do so by using the enableRevisionOnCreate() method in your definition of the getRevisionOptions() method.

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->enableRevisionOnCreate();
}
Limit the revisions

You can limit the number of revisions each model record can have by using the limitRevisionsTo() method in your definition of the getRevisionOptions() method.

This prevents ending up with thousands of revisions for a heavily updated record.

When the limit is reached, after creating the new (latest) revision, the script automatically removes the oldest revision that model record has.

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->limitRevisionsTo(100);
}
Revision only certain fields

If you don't want to revision all the model's fields (attributes), you can manually specify which fields you wish to store when creating a new revision, by using the fieldsToRevision() method in your definition of the getRevisionOptions() method.

Please note that the fields omitted won't be stored when creating the revision, but when rolling back to a revision, those ignored fields will become null / empty for the actual model record.

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->fieldsToRevision('title', 'content');
}
Exclude certain fields from being revisioned

Opposed to the fieldsToRevision() method, if you want to exclude certain fields when making a revision of an Eloquent model, use the fieldsToNotRevision() method in your definition of the getRevisionOptions() method

Please note that the fieldsToRevision() takes precedence over the fieldsToNotRevision().
Don't use both of these methods in the same definition of the getRevisionOptions method.

Please note that the fields omitted won't be stored when creating the revision, but when rolling back to a revision, those ignored fields will become null / empty for the actual model record.

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->fieldsToNotRevision('title', 'content');
}
Include timestamps when creating a revision

By default, when creating a revision, the actual model's timestamps are automatically excluded from the actual revision data.

If you'd like to store the model's timestamps when creating a revision, please use the withTimestamps() method in your definition of the getRevisionOptions() method.

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->withTimestamps();
}
Revision relationships alongside the model record

More often than not you will want to create a full copy in time of the model record and this includes revisioning its relations too (especially child relations).

You can specify what relations to be revisioned alongside the model record by using the relationsToRevision() method in your definition of the getRevisionOptions() method.

Please note that when rolling back the model record to a past revision, the specified relations will also be rolled back to their state when that revision happened (this includes re-creating a relation record from ground up if it was force deleted along the way, or deleting any future added related records up until the revision checkpoint).

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->relationsToRevision('comments', 'author');
}
Disable creating a revision when rolling back

By default, when rolling back to a past revision, a new revision is automatically created. This new revision contains the model record's state before the rollback happened.

You can disable this behavior by using the disableRevisioningWhenRollingBack() method in your definition of the getRevisionOptions() method.

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->disableRevisioningWhenRollingBack();
}

Events

The revision functionality comes packed with two Eloquent events: revisioning and revisioned

You can implement these events in your Eloquent models as you would implement any other Eloquent events that come with the Laravel framework.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Karl456\Revisions\Options\RevisionOptions;
use Karl456\Revisions\Traits\HasRevisions;

class YourModel extends Model
{
    use HasRevisions;

    /**
     * Boot the model.
     *
     * @return RevisionOptions
     */
    public static function boot()
    {
        parent::boot();

        static::revisioning(function ($model) {
            // your logic here
        });

        static::revisioned(function ($model) {
            // your logic here
        });
    }
    
    /**
     * Get the options for revisioning the model.
     *
     * @return RevisionOptions
     */
    public function getRevisionOptions(): RevisionOptions
    {
        return RevisionOptions::instance();
    }
}

Credits

Security

If you discover any security related issues, please email andrei.badea@neurony.ro instead of using the issue tracker.

License

The MIT License (MIT). Please see LICENSE for more information.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

karl456/laravel-revisions 适用场景与选型建议

karl456/laravel-revisions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「model」 「laravel」 「relations」 「eloquent」 「changes」 「copy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 karl456/laravel-revisions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 karl456/laravel-revisions 我们能提供哪些服务?
定制开发 / 二次开发

基于 karl456/laravel-revisions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-04