Giter Club home page Giter Club logo

laravel-revisionable's Introduction

JohannesSchobel/Laravel-Revisionable

Easy and conventient way to handle revisions of your models within the database.

  • Handles the revisions in bulk - one entry covers all the created/updated fields, what makes it really easy to e.g., compare 2 given versions or get all the data changed during one single transaction.

Requirements

  • This package requires PHP 5.4+
  • Currently it works out of the box with Laravel5.4 + generic Illuminate Guard, tymon/jwt-auth OR cartalyst/sentry 2/sentinel 2

Usage (Laravel 5 basic example - see Customization below as well)

1. Download the package or require in your composer.json:

composer require johannesschobel/laravel-revisionable

2. Add the service provider to your app/config/app.php:

    'providers' => array(
        ...
        'JohannesSchobel\Revisionable\RevisionableServiceProvider',
    ),

3. Publish the package config file:

~$ php artisan vendor:publish [--provider="JohannesSchobel\Revisionable\RevisionableServiceProvider"]

this will create config/revisionable.php file, where you can adjust a few settings.

4. Run the migration in order to create the revisions table:

~$ php artisan migrate

5. Add revisionable trait to the models you wish to keep track of:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use JohannesSchobel\Revisionable\Traits\Revisionable;

class User extends Model
{
    use Revisionable;
}    

And that's all to get you started!

Customization

The package offers a set of configuration options.

White-Listing Fields

If you would like to revision only specific fields of the model, you can define them like so:

    /*
     * Set revisionable whitelist - only changes to any
     * of these fields will be tracked during updates.
     */
    protected $revisionable = [
        'email',
        'name'
    ];

This way, only the fields email and name are tracked and store as revision in the database. The default behaviour is to revision all fields.

Disable Revisions

If you want to disable revisions for a specific model just add the following variables to your model

    protected $revisionEnabled = false;

Revision Cleanup

You can further specify that you only want to clean up old revisions of a model. By doing so, you can further define, how many revisions of one model you would like to keep in your database (default is set to 20). Say, if you add the 21st revision, the first one is deleted.

You may customize this behaviour for each model by changing the variables

    protected $revisionLimitCleanup = true; // only works with revisionLimit
    protected $revisionLimit = 50;  // keep 50 instead of 20 revisions of this model

Rollback (aka load old revisions)

Of course, you can rollback to an old revision of your model. The Trait added earlier (e.g., the Revisionable trait) already provides handy methods for you - so you don't need to worry about this.

You can either use the $model->rollbackToTimestamp($timestamp) or $model->rollbackSteps($steps) functions for this purpose. Note that there is a configuration flag revisionable.rollback.cleanup (default false) that indicates, whether the revisions rolled back should be deleted (true) or not (false).

Both functions return the rolled back model, which is already persisted in the database.

Demonstration

$ php artisan tinker

>>> $ticket = App\Models\Ticket::first();
=> <App\Models\Ticket>

>>> $revision->getDiff();
=> [
       "customer_id"    => [
           "old" => "1",
           "new" => "101"
       ],
       "item_id"        => [
           "old" => "2",
           "new" => "1"
       ],
       "responsible_id" => [
           "old" => "8",
           "new" => "2"
       ]
   ]

>>> $revision->old('item_id');
=> "2"

>>> $revision->new('item_id');
=> "1"

>>> $revision->isUpdated('item_id');
=> true

>>> $revision->isUpdated('note');
=> false

>>> $revision->label('item_id');
=> "item_id"

>>> $revision->old;
=> [
       "defect"         => "foo",
       "note"           => "bar",
       "customer_id"    => "1",
       "item_id"        => "2",
       "responsible_id" => "8",
       "status_id"      => "6"
   ]

>>> $revision->action;
=> "updated"

laravel-revisionable's People

Contributors

johannesschobel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mrookame xandy3br

laravel-revisionable's Issues

Can i Use this For Multi Auth ?

I Understand it Works out of the box, using User Model...

But What if i Have a Multi Auth Set up?

Lets Say i have user model, employee and client model or more...

How can i use this so that the proper model of the current authenticated user

is used?

Add more examples

E.g. it is not clear how to undo deletion

I can see the change (deletion) saved in revisions table, but how do I roll it back?

build a revision manager

am trying to build a revision manager similar to wp or nodebb, but sadly the docs are not clear enough and am not sure how things exactly works.

  • what is the reason for ip & ip_forwarded columns ?
  • how the package calculate the steps ?
  • how to getDiff of 2 versions, say version # 3 & current ?.

also can we disable the package for created events ?

fix php artisan command

php artisan vendor:publish [--provider="JohannesSchobel\Revisionable\RevisionableServiceProvider"]

leads to:

[Symfony\Component\Console\Exception\RuntimeException]  
  Too many arguments, expected arguments "command".

this works:
php artisan vendor:publish --provider="JohannesSchobel\Revisionable\RevisionableServiceProvider"

Not creating revisions table on Laravel 5.5

I followed the instructions as said in the documentation I have a revisionable in my config. But when I try to migrate it. It does not create a revisions table.

Here is some samle code.

use JohannesSchobel\Revisionable\Traits\Revisionable;

class AnalysisRequest extends Model
{
    use SoftDeletes;
    use Revisionable;
    
        public $table = 'analysis_requests';
        
        protected $revisionable = [
            'sample_description',
            'special_instruction',
            'rushable',
            'status'
        ];

        protected $revisionLimitCleanup = true; // only works with revisionLimit
        protected $revisionLimit = 50;  // keep 50 instead of 20 revisions of this model
    
        protected $dates = ['deleted_at', 'date_analyzed'];

is it possible to support json columns ?

atm am using https://github.com/spatie/laravel-translatable which save attribute values in locale codes in json column so for example

title: {"ar": "some arabic title", "en": "Lorem ipsum dolor sit amet."}

this cuz the package to not work correctly as it can't compare the diff between old & new,

screen shot 2017-10-23 at 12 35 56 pm

note the first item in 'title' & 'body'

so is there a way to first check the attribute with json_decode before saving the revision ?

i already fixed this issue for restoration, but not sure where to edit this part to allow json columns.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.