• Stars
    star
    260
  • Rank 157,189 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 10 years ago
  • Updated 9 months ago

Reviews

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

Repository Details

Nice and easy way to handle revisions of your db

Sofa/Revisionable

Code quality Latest Stable Version Downloads

Nice and easy way to handle revisions of your db.

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

Requirements

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

Usage (Laravel5 basic example - see Customization below as well)

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

composer require sofa/revisionable

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

    'providers' => array(

        ...

        'Sofa\Revisionable\Laravel\ServiceProvider',
    ),

3. Publish the package config file:

~$ php artisan vendor:publish [--provider="Sofa\Revisionable\Laravel\ServiceProvider"]

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

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | User model (for executor relation on Revision model).
    |--------------------------------------------------------------------------
    |
    | By default App\User.
    */
    'usermodel' => 'App\User',

    /*
    |--------------------------------------------------------------------------
    | User provider (auth) implementation.
    |--------------------------------------------------------------------------
    |
    | By default Laravel generic Illuminate\Auth\Guard.
    |
    | Supported options:
    |  - illuminate
    |  - sentry
    |  - sentinel
    |  - jwt-auth
    |  - session 
    */
    'userprovider' => 'illuminate',


    /*
    |--------------------------------------------------------------------------
    | User field to be saved as the author of tracked action.
    |--------------------------------------------------------------------------
    |
    | By default:
    |
    |  - id for illuminate
    |  - login field (email) for sentry/sentinel
    */
    'userfield' => 'id',


    /*
    |--------------------------------------------------------------------------
    | Table used for the revisions.
    |--------------------------------------------------------------------------
    */
    'table' => 'revisions',


    /*
    |--------------------------------------------------------------------------
    | Database connection used for the revisions.
    |--------------------------------------------------------------------------
    */
    'connection' => null,

];

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

~$ php artisan revisions:table
~$ php artisan revisions:upgrade-5.3
~$ php artisan migrate [--database=custom_connection]

You can provide additional --database param if you want the migration to be run using non-default db connection.

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

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Sofa\Revisionable\Laravel\Revisionable; // trait

class User extends Model
{
    use Revisionable;

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

And that's all to get your started!

Customization in L5

Default behaviour:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Sofa\Revisionable\Laravel\Revisionable;

class Ticket extends Model
{
    use Revisionable;
}
$ 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"         => "nie dziala",
       "note"           => "wrocilo na gwarancji",
       "customer_id"    => "1",
       "item_id"        => "2",
       "responsible_id" => "8",
       "status_id"      => "6"
   ]

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

But here's where you can leverage bundled Presenter in order to make useful adjustments:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Sofa\Revisionable\Laravel\Revisionable;

class Ticket extends Model
{
    use Revisionable;

    protected $revisionPresenter = 'App\Presenters\Revisions\Ticket';
}
namespace App\Presenters\Revisions;

use Sofa\Revisionable\Laravel\Presenter;

class Ticket extends Presenter
{
    protected $labels = [
        'item_id'        => 'Przedmiot',
        'customer_id'    => 'Klient',
        'status_id'      => 'Status',
        'responsible_id' => 'Serwisant',
        'defect'         => 'Usterka',
        'note'           => 'Uwagi',
    ];

    protected $passThrough = [
        'item_id'        => 'item.name',
        'customer_id'    => 'customer.name',
        'responsible_id' => 'serviceman.name',
        'status_id'      => 'status.name',
    ];

    protected $actions = [
        'created'  => 'utworzony',
        'updated'  => 'edytowany',
        'deleted'  => 'usunięty',
        'restored' => 'przywrócony',
    ];

}

then

$ php artisan tinker

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

>>> $revision->old('item_id'); // value fetched from the relationship
=> "komputer pc"

>>> $revision->new('item_id'); // value fetched from the relationship
=> "laptop acer"

>>> $revision->label('item_id'); // custom label defined in the presenter
=> "Przedmiot"

>>> $revision->action; // custom action name defined in the presenter
=> "edytowany"

More Repositories

1

eloquence

Extensions for the Eloquent ORM
PHP
1,087
star
2

eloquence-base

base for the Eloquence extensions + Searchable
PHP
77
star
3

hookable

Laravel Eloquent hooks system
PHP
64
star
4

eloquence-mappable

Eloquence Mappable extension for Eloquent ORM
PHP
50
star
5

model-locking

Pseudo pessimistic model locking with broadcasted events for Laravel Eloquent ORM. https://softonsofa.com
PHP
47
star
6

Eloquent-triple-pivot

Laravel/Eloquent triple model many-to-many relation
PHP
43
star
7

eloquence-metable

Eloquence Metable extension for Eloquent ORM
PHP
32
star
8

laravel-global-scope

Define Laravel Global Scopes easier.
PHP
28
star
9

eloquence-mutable

Eloquence Mutable extension for Eloquent ORM
PHP
27
star
10

sublime-snippets

A few useful Sublime Text snippets
24
star
11

eloquence-validable

Eloquence Validable extension for Eloquent ORM
PHP
24
star
12

eloquent-cascade

Cascading soft / hard deletes for Laravel Eloquent ORM https://softonsofa.com
PHP
24
star
13

laravel-kahlan

Laravel context for kahlan specs/tests - allows nice BDD way of developing Laravel app. https://softonsofa.com
PHP
16
star
14

laravel-test-generator

Don't spend time writing boilerplate for your feature tests in Laravel app.
PHP
11
star
15

eloquent-testsuite

Helpers for fast and reliable UNIT tests for your Eloquent Models with PHPUnit https://softonsofa.com
PHP
10
star
16

laravel5-global-scope-example

Learn how to define and use Eloquent Global Scopes in Laravel 5
CSS
10
star
17

eloquent-scopes

Handy scopes for the eloquent (Laravel) query builder.
PHP
8
star
18

state-machine

Easy to use Finite State Machine implementation https://softonsofa.com
PHP
7
star
19

laravel-db-queries-alert

protect your Laravel app from N+1 and alikes AKA check your queries bro!
PHP
7
star
20

laravel-history

Plug-and-play full history for your Eloquent models
PHP
7
star
21

laravel-scopes

Handy scopes for the Laravel's Query Builder
PHP
4
star
22

kahlan-driven-laravel

Usage example of the https://github.com/jarektkaczyk/laravel-kahlan - a laravel context in kahlan specs. https://softonsofa.com
PHP
4
star
23

artisan-log

PHP
3
star
24

laravel4-global-scope-example

PHP
2
star
25

laravel-searchable

Extension for Laravel Query Builder providing simple yet powerful fulltext search
PHP
2
star
26

laravel-sortable

Easily sort your eloquent models https://softonsofa.com
PHP
1
star
27

action-labeler

JavaScript
1
star
28

php-json

A simple wrapper around json_encode() and json_decode() for catching any errors without executing json_last_error()
PHP
1
star