wearesweet/laravel-revisonable

Apply the trait HasRevisions to any model to be able to: View a history of changes, revert to previous versions of the model and allow for easy moderation of changes before persisting to the database.

dev-master 2020-07-29 09:13 UTC

This package is auto-updated.

Last update: 2024-09-29 05:48:52 UTC


README

Latest Stable Version MIT Licensed GitHub Tests Action Status Quality Score Total Downloads

Apply the trait HasRevisions to any model to be able to: View a history of changes, revert to previous versions of the model and allow for easy moderation of changes before persisting to the database.

About We Are Sweet

We Are Sweet is a small but mighty wb agency providing design development and consultancy services to a wide range of clients.

Check us out

Installation and usage

This package requires PHP 7.6 and Laravel 6 or higher.

Run

$ composer require wearesweet/laravel-revisionable

HasRevisions Trait

Add the HasRevisions trait to your model(s).

class User {
    use WeAreSweet\LaravelRevisionable\Traits\HasRevisions;
}

Listen for certain attributes

Override revisionableAttributes so that it returns an array of attributes you want to allow revisions for.

If using revisionableAttributes in conjunction with persistRevisions, attributes in your array will be stored as a revision which can later be persisted (approved) and all other attributes not in the array will be persisted imminently.

class User {
    use WeAreSweet\LaravelRevisionable\Traits\HasRevisions;
    
    public function revisionableAttributes()
    {
        return 'name';
    }
}

Prevent persisting model data to the database unless approved

You can tell your models not to save directly to the model and instead save the data in revisions which can be applied to the model later.

This is useful for storing draft states of your models or for moderating and approving changes.

To do this, add the method persistRevisions to your class and have it return false.

class User {
    use WeAreSweet\LaravelRevisionable\Traits\HasRevisions;
    
    public function persistRevisions()
    {
        return false;
    }
}

View all models revision history

App\User::first()->revisions;

View pending model revisions

App\User::first()->revisions()->pending()->get();

Approve a revision

App\User::first()->revisions->first()->approve();

View merged pending revisions data

App\User::first()->pendingRevisions();

Example input/output

Input:
{
  "user":{
    "revisions": [
      {
        "date": "2020",
        "data": {"name": "Ben"}
      },
      {
        "date": "2010",
        "data": {"name": "Neb", "age":  23}
      }
    ]
  }
}
Output:
{
  "name": "Ben",
  "age": 23
}

Approve all model revisions

App\User::first()->approveAllRevisions();

Testing

Run the tests with:

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email developers@wearesweet.co.uk instead of using the issue tracker.

Credits

License

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