• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

mpociot/versionable: Laravel Model versioning made easy

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

mpociot/versionable

开源软件地址(OpenSource Url):

https://github.com/mpociot/versionable

开源编程语言(OpenSource Language):

PHP 100.0%

开源软件介绍(OpenSource Introduction):

Versionable

Laravel Model versioning made easy

image image image codecov.io Scrutinizer Code Quality Build Status

Keep track of all your model changes and revert to previous versions of it.

// Restore to the previous change
$content->previousVersion()->revert();

// Get model from a version
$oldModel = Version::find(100)->getModel();

Installation

You can install via composer:

composer require mpociot/versionable

Run the migrations.

php artisan migrate --path=vendor/mpociot/versionable/src/migrations

Alternatively, publish the migrations.

php artisan vendor:publish --provider="Mpociot\Versionable\Providers\ServiceProvider" --tag="migrations"

Then customize and run them.

php artisan migrate

Usage

Let the Models you want to set under version control use the VersionableTrait.

class Content extends Model {
	
	use Mpociot\Versionable\VersionableTrait;
	
}

That's it!

Every time you update your model, a new version containing the previous attributes will be stored in your database.

All timestamps and the optional soft-delete timestamp will be ignored.

Exclude attributes from versioning

Sometimes you don't want to create a version every time an attribute on your model changes. For example your User model might have a last_login_at attribute. I'm pretty sure you don't want to create a new version of your User model every time that user logs in.

To exclude specific attributes from versioning, add a new array property to your model named dontVersionFields.

class User extends Model {
	
	use Mpociot\Versionable\VersionableTrait;
	
	/**
	 * @var array
	 */
	protected $dontVersionFields = [ 'last_login_at' ];

}

Hidden fields

There are times you might want to include hidden fields in the version data. You might have hidden the fields with the visible or hidden properties in your model.

You can have those fields that are typically hidden in the rest of your project saved in the version data by adding them to the versionedHiddenFields property of the versionable model.

class User {

    use VersionableTrait;

    // Typically hidden fields
    protected $hidden = ['email', 'password'];

    // Save these hidden fields
    protected $versionedHiddenFields = ['email', 'password'];

}

Maximum number of stored versions

You can control the maximum number of stored versions per model. By default, there will be no limit and all versions will be saved. Depending on your application, this could lead to a lot of versions, so you might want to limit the amount of stored versions.

You can do this by setting a $keepOldVersions property on your versionable models:

class User {

    use VersionableTrait;

    // Keep the last 10 versions.
    protected $keepOldVersions = 10;

}

Retrieving all versions associated to a model

To retrieve all stored versions use the versions attribute on your model.

This attribute can also be accessed like any other Laravel relation, since it is a MorphMany relation.

$model->versions;

Getting a diff of two versions

If you want to know, what exactly has changed between two versions, use the version model's diff method.

The diff method takes a version model as an argument. This defines the version to diff against. If no version is provided, it will use the current version.

/**
 * Create a diff against the current version
 */
$diff = $page->previousVersion()->diff();

/**
 * Create a diff against a specific version
 */
$diff = $page->currentVersion()->diff( $version );

The result will be an associative array containing the attribute name as the key, and the different attribute value.

Revert to a previous version

Saving versions is pretty cool, but the real benefit will be the ability to revert to a specific version.

There are multiple ways to do this.

Revert to the previous version

You can easily revert to the version prior to the currently active version using:

$content->previousVersion()->revert();

Revert to a specific version ID

You can also revert to a specific version ID of a model using:

$revertedModel = Version::find( $version_id )->revert();

Disable versioning

In some situations you might want to disable versioning a specific model completely for the current request.

You can do this by using the disableVersioning and enableVersioning methods on the versionable model.

$user = User::find(1);
$user->disableVersioning();

// This will not create a new version entry.
$user->update([
    'some_attribute' => 'changed value'
]);

Use different version table

Some times we want to have models versions in differents tables. By default versions are stored in the table 'versions', defined in Mpociot\Versionable\Version::$table.

To use a different table to store version for some model we have to change the table name. To do so, create a model that extends Mpociot\Versionable\Version and set the $table property to another table name.

class MyModelVersion extends Version
{
    $table = 'mymodel_versions';
    // ...
}

In the model that you want it use this specific versions table, use the VersionableTrait Trait and add the property $versionClass with value the specific version model.

class MyModel extends Eloquent
{
    use VersionableTrait ;
    protected $versionClass = MyModelVersion::class ;
    // ...
}

And do not forget to create a migration for this versions table, exactly as the default versions table.

License

Versionable is free software distributed under the terms of the MIT license.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap