• Stars
    star
    687
  • Rank 65,799 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Laravel Model versioning made easy

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.

Adding versions to existing data

Versionable creates a version on update() of the updated model. So, if you're installing this on an already existing application, you may want to create a version of the current model:

    $model->createInitialVersion();

If no version exists, this will create the initial version.

If you want to do this for all instances of a model:

    Model::initializeVersions();

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.

More Repositories

1

chatgpt-vscode

A VSCode extension that allows you to use ChatGPT
TypeScript
3,779
star
2

laravel-apidoc-generator

Laravel API Documentation Generator
PHP
3,386
star
3

whiteboard

Simply write beautiful API documentation.
JavaScript
1,224
star
4

teamwork

User to Team associations with invitation system for the Laravel 5 Framework
PHP
1,036
star
5

laravel-test-factory-helper

Generate Laravel test factories from your existing models
PHP
930
star
6

laravel-testtools

Chrome extension to generate Laravel integration tests while using your app.
JavaScript
473
star
7

documentarian

Create beautiful API documentation
JavaScript
377
star
8

captainhook

Add Webhooks to your Laravel app, arrr
PHP
335
star
9

reauthenticate

Reauthenticate users by letting them re-enter their passwords for specific parts of your app.
PHP
296
star
10

laravel-firebase-sync

Synchronize your Eloquent models with a Firebase Realtime Database.
PHP
264
star
11

laravel-face-auth

Laravel Face authentication
PHP
157
star
12

TiSideMenu

iOS 7 / 8 style side menu with parallax effect. (Wrapper module for RESideMenu)
Objective-C
147
star
13

pipeline

Simple PHP pipelines
PHP
131
star
14

reanimate

Easily add an "undo" option to your Laravel application
PHP
121
star
15

titanium-facebook-slide-menu

Example of how to implement a facebook like sliding menu in appcelerator's titanium mobile
JavaScript
117
star
16

blacksmith

The unofficial Laravel Forge PHP API
PHP
103
star
17

human-regex

Regular expressions for human beings, not machines
PHP
102
star
18

reflection-docblock

Fork of phpdocumentor/reflection-docblock to work with the api-documentation-generator
PHP
85
star
19

TiMotionEffects

UIMotionEffects for Appcelerator Titanium
Python
59
star
20

TiMWKProgressIndicator

A minimal progress indicator for iOS with status update support. Displays above UINavigationBar and below the Status Bar.
Objective-C
49
star
21

TiCircularSlider

Titanium circular slider module with many customizations.
Objective-C
47
star
22

TiCircularProgress

Circular progress indicator for Appcelerator Titanium
Objective-C
44
star
23

cockpit

Custom widgets for your Laravel app
PHP
38
star
24

titanium-social-modul

iOS6 module for facebook, twitter, sina weibo to use with appcelerator titanium
Python
38
star
25

laravel-slack-inviter

A little Laravel application to invite a user into your Slack team.
PHP
36
star
26

TiImageFromGIF

Create native imageviews from GIFs
Objective-C
35
star
27

TiCustomTab

Create Tabs with custom selected / unselected images.
Objective-C
34
star
28

laravel-composite-key

PHP trait to use composite keys in your Laravel Eloquent models.
PHP
34
star
29

codeception-testtools

Chrome extension to generate Codeception acceptance tests while using your app.
JavaScript
34
star
30

TiAndroidAutofocus

Prevents TextFields in Android to autofocus
Java
31
star
31

titanium-mobile-twitter-module

Basic twitter iOS 5 integration as a titanium module
Objective-C
30
star
32

TiAppirater

Titanium mobile iOS rating reminder
Objective-C
27
star
33

TiFaceRecognizer

CoreImage face detection module to use with the Appcelerator Titanium SDK
Python
27
star
34

titanium-mobile-alasset-module

Titanium module to get access to the iOS ALAsset framework
Objective-C
26
star
35

TiiMMAppSwitcher

iOS Titanium Mobile module - Replace iOS7 app switcher view with your own customized card view.
Objective-C
26
star
36

titanium-mobile-zip-module

zip module for appcelerators titanium mobile
C
25
star
37

chuck-norris-jokes

Create random Chuck Norris jokes.
PHP
25
star
38

TiScreenshotDetection

Detect when someone takes a screenshot in your Appcelerator Titanium iOS app.
Objective-C
20
star
39

TokenCompleteTextView

Java
20
star
40

lastfm-slack

Set your Last.fm tracks as your Slack status
PHP
19
star
41

TiAdvancedSplitWindow

Appcelerator titanium module to make use of the MGSplitView functions, not used by the Titanium SDK
Python
19
star
42

socialite-slack

Slack OAuth2 Provider for Laravel Socialite
PHP
18
star
43

TiTutorialView

Path like tutorial view as a commonJS module for Appcelerator Titanium
JavaScript
18
star
44

TiWebviewFragment

Simple helper class that is a workaround for Webviews Crashing / rendering blank on Android 4.4 in Titanium
Java
17
star
45

laravel-firebase-sync-example

Example project demonstrating the usage of the laravel-firebase-sync package
PHP
17
star
46

TiSplashScreen

Path like splash screen animation
Python
15
star
47

TiSDSegmentedControl

This is a titanium module for Olivier Poitrey's SDSegmentedControl library.
Objective-C
14
star
48

TiDocumentInteraction

Share/Print/Open files with the native activity view controller in your Titanium app
Python
12
star
49

mpociot

10
star
50

TiSMPageControl

Titanium mobile iOS module for some funky ScrollableView customization
Objective-C
7
star
51

llamero

A GUI application to easily try out Facebook's LLaMA models.
JavaScript
6
star
52

botman-spark-demo

BotMan + Cisco Spark demo project
PHP
5
star
53

psd2tss

JavaScript
5
star
54

feature-switch

A feature switcher for PHP
PHP
5
star
55

MagicPie

A Titanium implementation of https://github.com/Sk0rpion/MagicPie
Python
4
star
56

ti-crittercism-hook

A CLI hook for Appcelerator Titanium to automatically upload dSYM files to crittercism.
JavaScript
4
star
57

skeleton-php

PHP
3
star
58

TiIOS8WebviewFix

Prevent iOS8 iPad apps with WebViews from crashing
Python
3
star
59

laracon-eu-2017

My Laracon EU 2017 talk
PHP
3
star
60

php-uk-conference-2018

My PHP UK Conference 2018 Talk
PHP
3
star
61

titanium-twitter-oauth

A Twitter OAuth Module for Titanium Mobile
JavaScript
3
star
62

php-serbia-2018

The talk I gave at PHP Serbia 2018
PHP
2
star
63

ti-taffy

taffyDB wrapper for titanium
JavaScript
2
star
64

botman-workshop

PHP
2
star
65

TiShowcaseView

Native android module for Appcelerator Titanium that tries to wrap the "ShowcaseView" library.
2
star