PHPackages                             ramaid/laravel-auditor - 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. ramaid/laravel-auditor

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

ramaid/laravel-auditor
======================

A simple mongo activity logger to record various events of your laravel application

0153PHP

Since Aug 28Pushed 2y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Record your audit activity logs in Mongo DB on on Laravel
=========================================================

[](#record-your-audit-activity-logs-in-mongo-db-on-on-laravel)

[![Latest Stable Version](https://camo.githubusercontent.com/7f2286969046c989da54d5c325e3a4b04cbe2ac1231ff71ab6f2c797e1d391d8/68747470733a2f2f706f7365722e707567782e6f72672f6d736f6e6f77616c2f6c61726176656c2d61756469746f722f762f737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/msonowal/laravel-auditor)[![Total Downloads](https://camo.githubusercontent.com/ced7830b041c5374497f3913e553255b292619a3e5c3356a2aceedbcb369e1c6/68747470733a2f2f706f7365722e707567782e6f72672f6d736f6e6f77616c2f6c61726176656c2d61756469746f722f646f776e6c6f6164733f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/msonowal/laravel-auditor)[![Daily Downloads](https://camo.githubusercontent.com/c701d29c32c434be07a5acc2cd9a63485cd620ab5937e46260a0703bb3d11a47/68747470733a2f2f706f7365722e707567782e6f72672f6d736f6e6f77616c2f6c61726176656c2d61756469746f722f642f6461696c793f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/msonowal/laravel-auditor)[![License](https://camo.githubusercontent.com/4aafa14f70c3c43e60494f75b0103c9f20852980637f942a2984e9d266edabf1/68747470733a2f2f706f7365722e707567782e6f72672f6d736f6e6f77616c2f6c61726176656c2d61756469746f722f6c6963656e73653f666f726d61743d666c61742d737175617265)](https://github.com/msonowal/laravel-auditor/blob/master/LICENSE)[![StyleCI](https://camo.githubusercontent.com/36afabeccf6907cdf83835ff9a38fcb80f7304551144bfe409c5e65d69d756fd/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3136343133393534322f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/164139542)

A simple package to track, record and log changes of your laravel apps events and also Eloquent Models by Polymorphic relations. By Default, the Package stores all audit activity in the `audit_logs` collection in the Mongo DB. However you can customize everything via config. This package uses [jenssegers/mongodb](https://github.com/jenssegers/Laravel-MongoDB) for interacting with Mongo DB. By Default Users Ip address and User Agents are Captured for every request if it is performed by Users.

Installation
------------

[](#installation)

```
$ composer require msonowal/laravel-auditor
```

The package will automatically register itself.

Configuration
-------------

[](#configuration)

You can optionally publish the config file with:

```
php artisan vendor:publish --provider="Msonowal\Audit\AuditServiceProvider" --tag="config"
```

This is the contents of the published config file:

```
[

    /*
     * If set to false, no activities will be saved to the database.
     */
    'enabled' => env('AUDIT_ENABLED', true),

    /*
     * By default all the activities will be processed via queue
     * If set to false, all the activities will be processed instantly.
     */
    'use_queue' => env('AUDIT_MODE', true),

    /*
     * When the clean-command is executed, all recording activities older than
     * the number of days specified here will be deleted.
     */
    'delete_records_older_than_days' => 365,

    /*
     * When the clean-command is executed, all recording activities older than
     * the number of days specified above and if its beyond the max entries limit
     * those records will be deleted and mostly on the specified log name
     */
    'max_entries' => 50000,

    /*
     * If no log name is passed to the audit() helper
     * we use this default log name.
     */
    'default_log_name' => env('AUDIT_DEFAULT_LOG_NAME', 'default'),

    /*
     * If set to true, the subject returns soft deleted models.
     */
    'subject_returns_soft_deleted_models' => true,

    /*
     * This is the name of the database connection that will be used by the migration and
     * used by the Services.
     */
    'connection_name' => env('AUDIT_CONNECTION', 'activity'),
    /*
     * This is the name of the collection that will be created by the migration and
     * used by the AuditActivity model.
     */
    'collection_name' => env('AUDIT_COLLECTION_NAME', 'activity_logs'),
];
```

You can publish the migration with:

```
php artisan vendor:publish --provider="Msonowal\Audit\AuditServiceProvider" --tag="migrations"
```

*Note*: The default migration adds the indexes to the collection for the essentials fields however you can modify and tailor upto your needs.

After publishing the migration you can update the indexes on the `audit_logs` collection by running the migrations:

```
php artisan migrate
```

Here's a demo of how you can use it:

```
audit()->log('Something, has been done');
```

You can retrieve all activity using the `AuditServiceRepository` class.

Inject in your methods

```
function test(AuditServiceRepository $audit)
{
    $audit->all(); //this returns all the records in plain array directly from DB

    $audit->paginate(); //this returns all the records as a Model Instance with default 50 per page and all the fields
}
```

Here's a more advanced example:

```
audit()
   ->performedOn($anEloquentModel)
   ->causedBy($user)
   ->withProperties(['customProperty' => 'customValue'])
   ->add('Something, has been done');

$lastLoggedActivity = AuditActivityMoloquent::all()->last();

$lastLoggedActivity->subject; //returns an instance of an eloquent model
$lastLoggedActivity->causer; //returns an instance of your user model
$lastLoggedActivity->getExtraProperty('customProperty'); //returns 'customValue'
$lastLoggedActivity->description; //returns 'Look, I logged something'
```

Version
-------

[](#version)

According to the composer docs the [version](https://getcomposer.org/doc/04-schema.md#version):

> We will follow the format of X.Y.Z or vX.Y.Z with an optional suffix of -dev, -patch (-p), -alpha (-a), -beta (-b) or -RC. The patch, alpha, beta and RC suffixes can also be followed by a number. Examples:
>
> - 1.0.0
> - 1.0.2
> - 0.1.0
> - 0.2.5
> - 1.0.0-dev
> - 1.0.0-alpha3
> - 1.0.0-beta2
> - 1.0.0-RC5
> - v2.0.4-p1

Testing After install the dependencies you can run all the tests by excecuting the follow command:

```
$ vendor/bin/phpunit
```

The output should look similar to this:

```
.                                                                  1 / 1 (100%)

Time: 84 ms, Memory: 12.00MB

OK (1 test, 1 assertion)

```

All the test files should be inside the `tests/` directory. Here is an example:

```
