PHPackages                             anexia/laravel-changeset - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. anexia/laravel-changeset

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

anexia/laravel-changeset
========================

A laravel module to monitor model changes and log them into the database

1.1.0(5y ago)01341MITPHP

Since Apr 11Pushed 5y ago2 watchersCompare

[ Source](https://github.com/anexia-it/anexia-laravel-changeset)[ Packagist](https://packagist.org/packages/anexia/laravel-changeset)[ RSS](/packages/anexia-laravel-changeset/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

Anexia Changeset
================

[](#anexia-changeset)

A Laravel package used to detect permanent changes to your Eloquent models (written into the database) and log them into separate database tables.

Installation and configuration
------------------------------

[](#installation-and-configuration)

Install the module via composer, therefore adapt the `require` part of your `composer.json`:

```
"require": {
    "anexia/laravel-changeset": "1.1.0"
}

```

In the projects `config/app.php` add the new service provider. This will include the package's migration files to the project's migration path:

```
return [
    'providers' => [
        /*
         * Anexia Changeset Service Providers...
         */
        Anexia\Changeset\Providers\ChangesetServiceProvider::class,
    ]
];

```

Now run

```
composer update [-o]

```

to add the packages source code to your `/vendor` directory and update the autoloading.

### Custom Database Configuration

[](#custom-database-configuration)

By default the changeset tables get added into the application's main database. The default changeset db connection is 'changeset\_mysql', which is defined in the package's /config/connections.php:

```
// from package's /config/connections.php:

    'changeset_mysql' => [
        'driver' => 'mysql',
        'host' => env('CHANGESET_DB_HOST', env('DB_HOST', '127.0.0.1')),
        'port' => env('CHANGESET_DB_PORT', env('DB_PORT', '3306')),
        'database' => env('CHANGESET_DB_DATABASE', env('DB_DATABASE', 'forge')),
        'username' => env('CHANGESET_DB_USERNAME', env('DB_USERNAME', 'forge')),
        'password' => env('CHANGESET_DB_PASSWORD', env('DB_PASSWORD', '')),
        'unix_socket' => env('CHANGESET_DB_SOCKET', env('DB_SOCKET', '')),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

```

So the changeset db uses the 'DB\_' variables in .env by default. To use a different changeset db, another set of .env varibales can be configured:

```
# from .env:

CHANGESET_DB_CONNECTION=changeset_pgsql
CHANGESET_DB_HOST=127.0.0.1
CHANGESET_DB_PORT=5432
CHANGESET_DB_DATABASE=database
CHANGESET_DB_USERNAME=user
CHANGESET_DB_PASSWORD=pwd

```

The /config/connections.php comes with a preset variety of possible db configurations (changeset\_sqlite, changeset\_mysql, changeset\_pgsql, changeset\_sqlsrv), similar to Laravel's default db configurations. On application boot, these entries get added to the application's 'database.connections' configuration field.

If a different configuration is needed, it can always be defined in the usual Laravel manner in the application's /config/database.php and then set via the .env variables, e.g.:

```
// from application's /config/database.php
    'connections' => [
        ...

        'changeset_new' => [
            'driver' => 'mysql',
            'host' => env('CHANGESET_DB_HOST', env('DB_HOST', '127.0.0.1')),
            'port' => env('CHANGESET_DB_PORT', env('DB_PORT', '3306')),
            'db' => env('CHANGESET_DB_DATABASE', env('DB_DATABASE', 'forge')),
            'user' => env('CHANGESET_DB_USERNAME', env('DB_USERNAME', 'forge')),
            'pwd' => env('CHANGESET_DB_PASSWORD', env('DB_PASSWORD', '')),
            'socket' => env('CHANGESET_DB_SOCKET', env('DB_SOCKET', '')),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
    ],

```

```
# from .env:

CHANGESET_DB_CONNECTION=changeset_new

```

How it works
------------

[](#how-it-works)

The Changeset package comes with three new models:

```
Changeset
    * id
    * action_id (uniquid() for each request/user action; related changesets share the same action_id; not nullable)
    * changeset_type (I = INSERT, U = UPDATE, D = DELETE; not nullable)
    * display (info text that describes the changes within the changeset; not nullable)
    * object_type_id (references to the name of the class that got changed within the changeset; foreign_key to
        object_type; not nullable)
    * object_uuid (tracked identifier of the object; 'id' property by default; not nullable)
    * related_changeset_id (child changeset that triggered an additional changeset for a parent model; the parent model
        is defined by $trackRelated in the child class; foreign_key to changeset; nullable)
    * user_id (identifier of the user that triggered the change; must implement the ChangesetUserInterface; App\User by
        default; nullable)
    * created_at (timestamp of the changeset entry's creation in the database; auto-managed by Eloquent)
    * updated_at (timestamp of the changeset entry's last update in the database; auto-managed by Eloquent)

```

```
Changerecord
    * id
    * field_name (property that got changed; nullable)
    * changeset_id (One-To-Many relation to a changeset; one changeset can contain multiple changerecords; foreign_key
        to changeset; not nullable)
    * display (info text that describes the change on the field_name; not nullable)
    * is_deletion (boolean that marks a 'DELETE' event; 0 by default)
    * is_related (boolean that marks if the associated changeset has a related_changeset_id defined; 0 by default)
    * new_value (value of the field_name after the change; nullable)
    * old_value (value of the field_name before the change; nullable)
    * created_at (timestamp of the changerecord entry's creation in the database; auto-managed by Eloquent)
    * updated_at (timestamp of the changerecord entry's last update in the database; auto-managed by Eloquent)

```

```
ObjectType
    * id
    * name (class that is trackable = uses ChangesetTrackable trait; unique; not nullable)
    * created_at (timestamp of the object type entry's creation in the database; auto-managed by Eloquent)
    * updated_at (timestamp of the object type entry's last update in the database; auto-managed by Eloquent)

```

The Changeset package adds hooks to the models 'created', 'updated' and 'deleted' events. So any change (save(), update(), delete(), forceDelete()) of a trackable model (that uses the ChangesetTrackable trait, like Post.php) triggers a changeset creation.

Depending on the performed change, multiple changerecords get created and associated to the new changeset:

- If a new model gets saved, all its properties receive a changerecord that gets associated to its 'CREATE' changeset (since they were all 'changed').
- If an existing model gets updated, all the changed properties (different values than before the change) receive a changerecord that gets associated to its 'UPDATE' changeset.
- If an existing model gets deleted, no changerecord gets associated to its 'DELETE' changeset.

Regardless of the performed change's type, further changesets for configured parent relations (via $trackRelated) will be triggered. Parent relations themselves can define their own $trackRelated associations, so a multi-layer tree of related changesets might be triggered (see Usage 'Related Changesets').

Usage
-----

[](#usage)

Use The ChangesetTrackable trait in a base model class. All models that are supposed to be changeset-trackable must then be extended from this base model class.

```
// base model class app/BaseModel.php
