owen-it / laravel-auditing

Record the change log from models in Laravel
https://laravel-auditing.com
MIT License
3.05k stars 390 forks source link

How to implement audit on mongodb implemented model #823

Closed ks-dipeshc closed 1 year ago

ks-dipeshc commented 1 year ago

Hello guys, I use PostgreSQL as well as MongoDB in my project. Auditing on PostgreSQL implemented Model is working fine, but Auditing on MongoDB implemented model is not working.

Here is my config/audit.php file:

<?php
return [

    /*
    |--------------------------------------------------------------------------
    | Audit Implementation
    |--------------------------------------------------------------------------
    |
    | Define which Audit model implementation should be used.
    |
    */

    'implementation' => OwenIt\Auditing\Models\Audit::class,

    /*
    |--------------------------------------------------------------------------
    | User Morph prefix & Guards
    |--------------------------------------------------------------------------
    |
    | Define the morph prefix and authentication guards for the User resolver.
    |
    */

    'user' => [
        'morph_prefix' => 'user',
        'guards'       => [
            'api',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Audit Resolvers
    |--------------------------------------------------------------------------
    |
    | Define the User, IP Address, User Agent and URL resolver implementations.
    |
    */
    'resolver' => [
        'user'       => OwenIt\Auditing\Resolvers\UserResolver::class,
        'ip_address' => OwenIt\Auditing\Resolvers\IpAddressResolver::class,
        'user_agent' => OwenIt\Auditing\Resolvers\UserAgentResolver::class,
        'url'        => OwenIt\Auditing\Resolvers\UrlResolver::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Audit Events
    |--------------------------------------------------------------------------
    |
    | The Eloquent events that trigger an Audit.
    |
    */

    'events' => [
        'created',
        'updated',
        'deleted',
        'restored',
    ],

    /*
    |--------------------------------------------------------------------------
    | Strict Mode
    |--------------------------------------------------------------------------
    |
    | Enable the strict mode when auditing?
    |
    */

    'strict' => false,

    /*
    |--------------------------------------------------------------------------
    | Audit Timestamps
    |--------------------------------------------------------------------------
    |
    | Should the created_at, updated_at and deleted_at timestamps be audited?
    |
    */

    'timestamps' => false,

    /*
    |--------------------------------------------------------------------------
    | Audit Threshold
    |--------------------------------------------------------------------------
    |
    | Specify a threshold for the amount of Audit records a model can have.
    | Zero means no limit.
    |
    */

    'threshold' => 0,

    /*
    |--------------------------------------------------------------------------
    | Audit Driver
    |--------------------------------------------------------------------------
    |
    | The default audit driver used to keep track of changes.
    |
    */

    'driver' => 'database',

    /*
    |--------------------------------------------------------------------------
    | Audit Driver Configurations
    |--------------------------------------------------------------------------
    |
    | Available audit drivers and respective configurations.
    |
    */

    'drivers' => [
        'database' => [
            'table'      => 'audits',
            'connection' => null,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Audit Console
    |--------------------------------------------------------------------------
    |
    | Whether console events should be audited (eg. php artisan db:seed).
    |
    */

    'console' => false,
];

Here is MonogDB implemented model:

namespace App\Models;

use Illuminate\Support\Arr;
use Jenssegers\Mongodb\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;

class Questionset extends Model implements Auditable
{
    use \OwenIt\Auditing\Auditable;

    public const COLLECTION = 'questionset';

    protected $collection = self::COLLECTION;
    protected $connection = 'mongodb';

    public const CREATED_AT = "created";
    public const UPDATED_AT = "updated";

    protected $auditInclude = [
        self::UPDATED_AT,
        'attributes',
        'nodes'
    ];

    protected $auditEvents = [
        'updated',
        'deleted',
    ];
}
angeljqv commented 1 year ago

owen-it/laravel-auditing/mongodb

aromka commented 1 year ago

You'll need to add HybridRelations trait to your model.

ie.

...
use Jenssegers\Mongodb\Eloquent\HybridRelations;

class Questionset extends Model implements Auditable
{
    use \OwenIt\Auditing\Auditable, HybridRelations;

    ...
}